JVM memory model and partition

During program execution, the Java virtual machine divides the memory of the jvm into several different data areas for management. These areas have their own purposes, as well as creation and destruction times.
The memory area managed by jvm includes the following areas:
Write picture description here

Stack area:
the stack is divided into java virtual machine stack and local method stack

  • The focus is on the Java virtual machine stack, which is private to the thread and has the same life cycle as the thread.
  • Each method execution will create a stack frame for storing local variable table, operation stack, dynamic link, method exit, etc. Each method is called until it is executed. Corresponds to the process of a stack frame from pushing to popping in the virtual machine.
  • Generally speaking, the stack refers to the part of the local variable table , which stores the 8 basic data types known during compilation, as well as object references and instruction addresses. The local variable table is allocated during compilation . When entering a method, the memory size allocated for the local variables in the stack is determined.
  • There will be two exceptions StackOverFlowError and OutOfMemoneyError. When the thread request stack depth is greater than the depth allowed by the virtual machine, a StackOverFlowError error will be thrown; the virtual machine stack is dynamically expanded, and when the expansion cannot apply for enough memory space, an OutOfMemoneyError is thrown.
  • The local method stack is used by the virtual machine to serve the local method (native)

Heap area:

  • The heap is an area shared by all threads and created when the virtual machine starts for the sole purpose of storing object instances .
  • The heap area is the main area of ​​GC, and is usually divided into two blocks, the young generation and the old generation. In a more detailed way, the young generation is divided into the Eden area where newly created objects should be placed most. From survivor and To survivor save objects that survived GC. By default, the ratio is 8:1:1 respectively.
    However, many articles are divided into three blocks, and the method area is regarded as the permanent generation. This is probably based on the Hotspot virtual machine division, and then for example, IBM j9 does not have an introduction to permanent generation. No matter how partitioned, it is to store object instances.
  • There will be an exception OutOfMemoneyError

Method area:

  • The area shared by all threads is used to store data such as class information, constants, and static variables that have been loaded by the virtual machine . Described by the Java virtual machine as a logical part of the heap. It is customary to also call it the permanent generation (permanment generation)
  • Garbage collection rarely visits this area, but it also needs to be recycled, mainly for constant pool recycling and type unloading.
  • The constant pool is used to store various bytecodes and symbol references generated during compilation . The constant pool has certain dynamics and can store constants generated during compilation; constants during runtime can also be added to the constant pool, such as string intern() method.

Program counter :

  • An indicator of the line number being executed by the current thread. The next instruction is determined by changing the value of the counter, such as loops, branches, jumps, exception handling, thread recovery, etc., all rely on the counter to complete.
  • Java virtual machine multithreading is realized by switching threads in turn and allocating processor execution time. In order for the thread switch to recover to the correct position, each thread needs an independent program counter , so it is thread-private.
  • The only block of the Java virtual machine that does not specify any OutofMemoryError

The jvm partition is roughly this block, and there are many details in it, and the algorithm of each module is very complicated. Here is just a brief introduction to the partition to master some basic knowledge points.

Guess you like

Origin blog.csdn.net/m0_67265464/article/details/126743758