Java memory management principle and memory region details, java Memory Management
Java memory is often divided into Heap memory and Stack memory. This method is rough, and the Division of Java memory areas is actually far more complex than this. The popularity of this division method only shows that the memory areas most concerned by most programmers and most closely related to object memory allocation are these two. The "heap" refers to Java heap, and the "stack" refers to the Virtual Machine stack or the local variable table part of the Virtual Machine stack.
The memory managed by the Java Virtual Machine will include the following runtime data areas, as shown in:
Program countersThe program counter is a small memory space and can be seen as the row number indicator of the bytecode executed by the current thread. In the virtual machine conceptual model, when the bytecode interpreter is working, it selects the next bytecode instruction to be executed by changing the counter value, this counter is required for basic functions such as branch, loop, jump, exception handling, and thread recovery. Java Virtual Machine multithreading is implemented by switching threads in turn and allocating the processor execution time. A single processor (for a multi-core processor, It is a kernel) at any specific time) only the commands in one thread are executed. Therefore, in order to restore the thread to the correct execution location after switching, the memory is stored independently. We call this type of region as "thread-private" memory. If the thread is executing a Java method, this counter records the address of the Virtual Machine bytecode instruction being executed; if the Native method is being executed, the counter value is undefined ). This memory region is the only region in which the Java Virtual Machine specification does not specify any OutOfMemoryError conditions.
Java Virtual Machine StackLike program counters, Java Virtual Machine stacks are also proprietary to threads and have the same lifecycle as threads. The Virtual Machine stack describes the Memory Model of Java method execution. Each method creates a stack frame during execution to store information such as the local variable table, operand stack, dynamic link, and method exit. The process of calling each method until the execution is completed corresponds to the process of putting a stack frame into the stack to the stack. The local variable table stores various basic data types (boolean, byte, char, short, int, float, long, and double) and object references (reference type, it is not the same as the object itself. It may be a reference pointer pointing to the starting address of the object, or it may point to a handle representing the object or other locations related to the object) and returnAddress (pointing to the address of a bytecode instruction ). Data of the 64-bit long and double types occupy two local variable spaces (slots), and the remaining data types only occupy one space. The memory space required by the local variable table is allocated during compilation. when you enter a method, the size of the local variable space allocated in the frame is completely determined, the table size of local variables is not changed during method running. In the Java Virtual Machine specification, two exception conditions are specified for this region: If the stack depth requested by the thread is greater than the depth allowed by the virtual machine, an StackOverflowError exception will be thrown; if the Virtual Machine stack can be dynamically expanded and the memory cannot be applied for during expansion, an OutOfMemoryError error will be thrown.
Local method StackThe local method stack plays a very similar role with the Virtual Machine stack. The difference is that the virtual machine stack executes the Java method service for the virtual machine, and the local method stack serves the Native method used by the virtual machine. In the virtual machine specification, there is no mandatory rule on the language, usage method, and data structure of the method used in the local method stack. Therefore, the specific virtual machine can freely implement it. Even some virtual machines (such as Sun HotSpot virtual machines) directly combine the local method stack with the Virtual Machine stack. Like the Virtual Machine stack, StackOverflowError and OutOfMemoryError are thrown in the local method stack area.
Java heapFor most applications, Java heap is the largest memory managed by Java virtual machines. Java heap is a memory area shared by all threads. It is created when the VM is started. The only purpose of this memory area is to store object instances. Memory is allocated here for almost all object instances and arrays. Java heap is the main area for managing the Garbage Collector. Therefore, it is often called the "GC Heap ". From the perspective of memory allocation, since the collectors currently basically use generational collection algorithms, Java heap can also be subdivided into: New Generation and old generation; the details include Eden space, From other vor space, and To another vor space. From the perspective of memory allocation, the Java heap shared by threads may be divided into private allocation buffers for multiple threads. However, no matter how it is divided, it is irrelevant to the storage content. In any region, the storage is still an object instance. The further Division aims to better recycle the memory or allocate the memory faster. According to the Java Virtual Machine specification, the Java heap can be in physically discontinuous memory space, as long as the logic is continuous. In implementation, it can be either fixed or scalable, however, the current mainstream virtual machines are implemented according to scalability (through-Xmx and-Xms control ). If the heap does not have memory for instance allocation and the heap cannot be extended, an OutOfMemoryError is thrown.
Method AreaThe method area is the same as the Java heap. It is the memory area shared by various threads. It is used to store data such as class information, constants, static variables, and Code Compiled by the real-time compiler. The method area is created when the VM is started. Although the method area is a logical component of the heap, you can choose not to recycle garbage in this area for Simple Virtual Machine implementation. The Java virtual machine specification does not limit the memory location of the implementation method area and the management policy for compiling code. The capacity of the method area can be fixed or dynamically expanded as the program needs to be executed, without automatically shrinking when there is too much space. The method area can be discontinuous in the actual memory space. According to Java Virtual Machine specifications, when the method area cannot meet the memory allocation requirements, an OutOfMemoryError exception will be thrown.
Runtime constant poolThe runtime Constant Volume pool is part of the method area and is used to store various literal and symbolic references generated during the compilation period, this part of content will be stored in the runtime pool of the method area after the class is loaded. In general, in addition to saving the symbolic references described in the Class file, the translated direct references are also stored in the runtime pool. Another important feature of the runtime constant pool relative to the Class file constant pool is that it is dynamic. Java does not require constants to be generated only during compilation, that is to say, it is not the content of the constant pool preset into the Class file that can enter the constant pool for running in the method area. During running, new constants may also be placed in the pool, the intern () method of the String class is used by developers. Since the runtime constant pool is part of the method area, it is naturally restricted by the method Area Memory. When the constant Pool cannot be applied to the memory again, an OutOfMemoryError exception is thrown.
References:Deep understanding of Java Virtual Machine: Advanced JVM features and Best Practices Java virtual machine specification (Java SE 7)