Understand the JAVA memory area of the JVM - the runtime data area

When the JVM executes a JAVA program, it divides the managed memory into several different data areas, and these areas have their own purposes, as well as creation and destruction times. Some areas exist with the startup of the virtual machine, and some areas are created and destroyed depending on the start and end of user threads. The following figure shows the data area of ​​the JAVA virtual machine runtime



1. Program Counter

      (1) The program counter is a small memory space, which can be regarded as the line number commander of the bytecode executed by the current program. In the conceptual model of the virtual machine, the bytecode interpreter selects the next bytecode instruction to be executed through the value of the counter when it works. Basic functions such as branching, looping, jumping, exception handling, and thread recovery all need to rely on done with a calculator.

      (2) Multithreading in the Java virtual machine is achieved by switching threads in turn and allocating processor execution time, so at any time, a processor (multi-core processor means a core) only executes one thread. Therefore, in order to restore the thread to the correct position, each thread needs to have an independent program counter. The program counters between each thread do not affect each other and are stored independently. We call this type of memory area "thread-private" memory. .

       (3) If the thread executes a JAVA method, the counter records: the address of the virtual machine bytecode instruction being executed. If the thread executes a Native method (a native method, also known as a native method, a Native Method is an interface that Java calls non-Java code), the value of the counter is empty. This type of memory area is the only one that does not have any specified OutOfMemoryError condition in the Java Virtual Machine


2. Virtual machine stack

        (1) The thread is private, the life cycle is the same as the thread cycle, the memory model of the Java method execution described by the virtual machine stack: each method will create a stack frame to store the local variable table, operand stack, dynamic link while executing , method exit and other information, a method call corresponds to the process of pushing and popping a stack frame in the virtual machine.

       (2) The local variable table stores various basic data types and object reference types (reference) and returnAddress (pointing to the address of a bytecode instruction) known at compile time. Among them, the data of Long and double types only occupy two local variable spaces, and the other data types only occupy one. The memory space required by the local variable table is allocated during compilation, and the size of the local variable table is not changed during method execution.

        (3) Two abnormal conditions are specified in this area, StackOverflowError (stack overflow error): the depth of the thread request stack is greater than the depth allowed by the virtual machine, OutOfMenmoryError (memory overflow error): if the virtual machine stack can be dynamically expanded, if If enough memory cannot be allocated during expansion, an exception will be thrown.


3. Native method stack

         (1) Similar to the role played by the virtual machine stack, the difference between them is that the virtual machine stack serves the Java method execution for the virtual machine, while the local method stack serves the Native method used by the virtual machine. In the virtual machine specification, there is no mandatory provision for the language, usage and data results of the methods in the native method stack, so the virtual machine can freely implement it. Some virtual machines even combine the virtual machine stack and the native method stack into one. Like the virtual machine, the local method stack area will also have a StackOverflowError (stack overflow error) OutOfMenmoryError (memory overflow error) exception.

4. Method area

        (1) Memory area shared by threads

        (2) Store data such as class information, constants, static variables, and code after the just-in-time compiler that have been loaded by the virtual machine.

        (3) OutOfMenmoryError (memory overflow error) when the memory allocation cannot be satisfied.

           4.1. Runtime constant pool

               (1) Part of the runtime method area of ​​the runtime constant pool, used to store literals and symbolic references generated at compile time.

               (2) It is dynamic and does not require constants to be generated only during recompilation, and new constants can also be put into the pool during runtime. (intern of String class)

               (3) OutOfMenmoryError (memory overflow error) when the memory allocation cannot be satisfied.

5. Heap

        (1) For most applications, the Java heap is the largest piece of memory managed by the Java virtual machine. The Java heap is a memory area shared by all threads. It is created when the virtual machine starts , and almost all object instances are Instances are allocated here. The description in the Java virtual machine specification is: all object instances and arrays must be allocated on the heap, but with the development of JIT and the gradual maturity of escape analysis technology, stack allocation and scalar replacement optimization technology will Causes some subtle changes, all objects are allocated on the heap and gradually become less "absolute"

        (2) The Java heap is the main area managed by the garbage collector

        (3) The Java virtual machine specification stipulates that the Java heap can be in a discontinuous memory space, as long as the logic is continuous. If there is no memory in the heap to complete the instance allocation, and the heap can no longer be expanded, an OutOfMenmoryError will be thrown (out of memory error)


6. Direct memory

        (1) Direct memory is not part of the Java Virtual Machine runtime memory, nor is it defined in the Java Virtual Machine Specification.

          (2) When the sum of each memory area is greater than the physical memory limit, an OutOfMenmoryError (memory overflow error) will occur during dynamic expansion.

Guess you like

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