Summary of Java VIRTUAL machine memory area
The Java VIRTUAL machine (JVM) is an abstract computer operating system that manages memory areas that can be roughly divided into stacks and heaps, similar to the classification of memory in C or C ++, but such a classification is too shallow for Java virtual machinesProgram counters, virtual machine stacks, local method stacks, heaps, and method areasAccording to whether they are thread private or shared, these regions can be divided into two categories, as described below.
Thread private memory area
1. Program Counter Register
A program counter is a small memory space that can be viewed as a line number indicator of the bytecode being executed by the current thread. Each thread has its own independent program counter, which, if the thread is executing a Java method, tracks the address of the VM bytecode instructions that are being executed, whereas if the Native method is being executed, the program counter is undipay. This memory region is the only one where the Java Virtual Machine specification does not specify any OutOfMemoryError cases.
2. VM Stack
The virtual stack is also thread-private and describes the in-memory model of Java method execution: When each method is executed, a Stack Frame will be created to store information such as local variation table, operand Stack, dynamic link, method exit, etc. The process from invocation to completion of each method corresponds to the process of loading and unloading a Stack Frame in the VIRTUAL machine Stack. Virtual machine in the stack frame, local variables is more familiar, also is called “stack” usually, the local variables to complete the required memory space at compile time distribution, when entering a method, this method need to be allocated in the stack frame is entirely sure how local variable space, the method does not change the size of the local variables during the run. StackOverflowError: The stack depth requested by the thread is greater than the stack depth allowed by the VIRTUAL machine, especially when methods are called recursively. OutOfMemoryError: An OutOfMemoryError will result if the virtual machine stack cannot meet the space requirement requested by the thread, even after dynamic scaling
3. Native Method Stack
The local method stack is similar to the virtual machine stack, but serves local methods, and some virtual machines combine the two regions. Exceptions are thrown in the local method stack in the same way as in the virtual machine stack.
2. Shared memory area
1. Heap
In general, the heap is the largest chunk of memory managed by the Java VIRTUAL machine. It is shared by all threads and created when the virtual machine is started. The heap is used to store object instances. The heap is also the primary area managed by the garbage collector, and is often referred to as the “GC heap.” From a memory reclamation point of view, the heap can also be subdivided into new generation and old generation, since collectors are now mostly generational. Further subdivision can be divided into: Eden space, From Survivor space, To Survivor space, etc. From the perspective of memory Allocation, the heap contributed by threads can also be divided into Thread Local Allocation Buffer (TLAB) for multiple threads. The heap can be a physically discontinuous space, as long as it is logically continuous, and the -xmx and -xMS parameters control the maximum and minimum size of the heap. An OutOfMemoryError is raised if the heap size is not sufficient.
2. Method Area
Used to store data such as class information, constants, static variables, jIT-compiled code that has been loaded by the virtual machine. The Java Virtual Machine specification describes the method area as a logical part of the Heap, but it has an alias called non-heap.
The method area also throws OutOfMemoryError.
A section of the method area is used to store various literal and symbolic references generated at compile time in the runtime constant pool when the class is loaded into the method area. It is important to note that constants are not only generated at compile time; new constants are also generated at run time and sent to the constant pool, as in the String intern() method.Copy the code
3. Direct Memory
Native direct memory is not part of the Java VIRTUAL machine’s run-time data region, but it can significantly improve performance in some scenarios because it avoids copying data back and forth between the Java heap and Native heap.
Direct memory also throws OutOfMemoryError.