Region division:
During the execution of the program, the Java virtual machine divides the memory into several areas with different functions, as shown below:
This figure lists the various areas divided into memory, among which
Thread-private ones: program counter, virtual machine stack, local methods Stack
Thread-shared: heap, method area
The following will introduce the functions and functions of each area one by one
Program Counter:
1. The program counter is mainly used to point to the bytecode line number being executed. Each thread has a separate program counter that is not shared with each other. By changing this counter Point to the bytecode line number to execute the program
2. Mechanisms such as loops, jumps, exception handling, etc. all rely on this counter to complete
3. If the program executes a java program , it points to the bytecode being executed. If the native method is executed, its value is empty
java virtual machine stack:
1. The virtual machine stack is private to the thread. Each method will create a stack frame when executing, which is used to store information such as local variable tables, method exits, dynamic links, and operand stacks. ,
2. The execution process of each method is a process from pushing the virtual machine stack into the stack to popping it out
Many people often say that Java memory is divided into heap memory and stack memory. The stack memory refers to the virtual machine stack
. The local variable table mentioned above mainly stores the basic data types (int\byte\char\long, etc.), object references, and returnaddress known to the compiler. Type
Extension:
Object reference (reference): Not the object itself, it may be a reference pointer pointing to the starting address of the object, it may be a handle representing the object or other related locations
Returnaddress: is not a type in the Java API and cannot be called by us. It points to the address of a bytecode instruction
Local Method stack
Similar to the virtual machine stack, it is the memory that serves native methods
Java heap
Heap memory is thread-shared memory. Its main purpose is to store objects and is also the main area managed by the garbage collector.
The Java heap is divided into the new generation and the old generation. The specific garbage collection mechanism will be introduced in subsequent articles
Method area (also called non-heap)
It is also a memory shared memory, used to store loaded class information, constants, static variables, code compiled by the just-in-time compiler, etc.
The runtime constant pool: is part of the method area and is used to store the code generated by the compiler. Various literals and symbol references
Direct memory:
Not part of the virtual machine runtime data area, not It belongs to the memory managed by the JVM
In NIO, you can use the native function to directly allocate direct memory, and operate it through the DirectByteBuffer object in java as a reference to this memory
Advantages: Saves memory Copying steps, faster efficiency
Disadvantages: difficult to control, easy to cause memory leaks
The above is the detailed content of JVM advanced features--Introduction to java memory structure area. For more information, please follow other related articles on the PHP Chinese website!