JVM principle | JVM memory model

JVM memory model

1 Overview

  1. Program counter: If the thread is executing a java method, the counter records the address of the virtual machine bytecode instruction. If it is native [low-level method], then the counter is empty. This memory area is the only area without OutOfMemoryError in the virtual machine specification .

  2. Virtual machine stack: The execution and end of the java method correspond to the stacking and popping of the stack frame.

    • Stack frame : used to store local variable table, operation stack, dynamic link, method exit and other information

    • The memory space required by the local variable table is allocated during compilation. When entering a method, how much local variable space this method needs to allocate on the stack is completely determined, and the size of the local variable table will not be changed during the running of the method.

    • Two types of exceptions may appear in the Java virtual machine stack:

      1. The stack depth requested by the thread is greater than the stack depth allowed by the virtual machine, and a StackOverflowError will be thrown.
      2. The virtual machine stack space can be dynamically expanded. When the dynamic expansion cannot apply for enough space, an OutOfMemory exception will be thrown.
  3. Native method stack: The role played by the native method stack is very similar to that of the virtual machine stack. The difference is that the virtual machine stack executes Java method (that is, bytecode) services, while the local method stack is the native method used by the virtual machine. The service may be c or c++ called by the bottom layer. When we open the jdk installation directory, we can see that there are also many files written in c, which may be the c code called by the native method.

  4. Heap : For most applications, the heap is the largest memory area managed by the Java virtual machine. Because the objects stored in the heap are shared by threads, a synchronization mechanism is also required when multiple threads are used . Therefore, we need to focus on understanding.

    The instance is in the heap (that is, the new object)

  5. Method area: It stores the information of each class that has been loaded; static variable information is also stored in the method area; it
    can be regarded as the metadata of the class, stored in the method area;

    The method area is shared by threads; when multiple threads use a class, and the class has not been loaded, there should be only one thread to load the class and let other threads wait;
    the size of the method area does not need to be fixed , JVM can be dynamically adjusted according to the needs of the application.

img

2. What are memory leaks and memory leaks

Briefly:

Overflow: The memory needed is greater than the memory given by the system

Leak: an object is not used but not recovered

Guess you like

Origin blog.csdn.net/weixin_40597409/article/details/115243862