Java Runtime Data Area Summary

This article is reproduced from: http://www.cnblogs.com/fengbs/p/7029013.html

      According to the "Java Virtual Machine Specification (Java SE 7 Edition)", the memory managed by the Java virtual machine will include the following runtime data areas:


1. Program Counter

      The Program Counter Register is a small memory space that can be seen as a line number indicator of the bytecode executed by the current thread. In the conceptual model of the virtual machine, when the bytecode interpreter works, it selects the next bytecode instruction to execute by changing the value of this counter.

      Each thread has an independent program counter.

      If executing a java method, this counter records the address of the virtual machine bytecode instruction being executed. If it is a native method, the counter is empty. This memory area is the only one that does not specify any OutOfMemoryError condition in the Java Virtual Machine Specification.

2. Java virtual machine stack

      It is also thread-private and describes the memory model of Java method execution: each method creates a stack frame (Stack Frame) to store information such as local variable table, operand stack, dynamic link, method exit and so on. A method corresponds to a stack frame.

      The local variable table stores various primitive types, object references, and returnAddress types (pointing to the address of a bytecode instruction). Among them, 64-bit long and double occupy two local variable spaces, and the others only occupy one.

      There are two specified exceptions: 1. The depth of the stack requested by the thread is greater than the depth allowed by the virtual machine, and a StackOverflowError exception will be thrown; 2. If the virtual machine can be dynamically expanded, if it cannot apply for enough memory during expansion, it will be Throws OutOfMemoryError exception.

3. Native method stack

      Similar to the Java virtual machine stack, the difference is that the native method stack serves Native methods.

4. Java heap

   It is the largest piece of memory managed by the Java virtual machine. Shared by all threads, created when the virtual machine starts. The only purpose of the heap area is to store object instances.

      The heap can be subdivided into the new generation and the old generation, which can be further subdivided into Eden space, From Survivor space, and To Survivor space.

      When the heap cannot be expanded, an OutOfMemoryError exception is thrown

5. Method area

      All threads share and store data such as class information, constants, static variables, and code compiled by the just-in-time compiler that have been loaded by the virtual machine.

      When the method area cannot meet the memory allocation requirements, an OutOfMemoryError is thrown

6. Runtime constant pool

      It is a part of the method area. In addition to the description information of the class version, field, method, interface, etc. in the Class file, there is also a constant pool (Const Pool Table), which is used to store various literals and values ​​generated during compilation. Symbolic reference. It is not the content that is preset into the constant pool in the Class file that enters the method runtime constant pool, and new constants may also be put into the pool during runtime. This feature is used more by developers. Intern( )method.

      When the method area cannot meet the memory allocation requirements, an OutOfMemoryError is thrown

7. Direct memory

      It is not part of the virtual machine runtime data area, nor is it a memory area defined in the Java Virtual Machine specification.

      JDK1.4 added NIO and introduced an I/O method based on channels and buffers. It can use the Native function library to directly allocate memory outside the heap, and then use a DirectByteBuffer object stored in the Java heap as a reference to this memory. to operate. This improves performance because it avoids copying data back and forth between the Java heap and the Native heap.

      When the sum of each memory area is greater than the physical memory limit, an OutOfMemoryError exception is thrown.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325898738&siteId=291194637