JVM code running logic
Understanding the execution process of a Java program in the JVM will help you to have an in-depth understanding of the JVM memory management and running mechanism.
Let us gradually analyze the execution logic of a Java program in the JVM through a simple Java code example, combining the various memory areas and execution processes of the JVM.
Sample code
public class HelloWorld { // Static variables are stored in the method area private static String greeting = "Hello, World!"; // Member variables are stored in the heap memory private int number; public HelloWorld(int number) { = number; } // The main method is stored in the method area, and local variables are on the stack public static void main(String[] args) { // Local variables are stored in the virtual machine stack int localNumber = 10; // Objects are stored in the heap, and reference variables are present in the stack HelloWorld hello = new HelloWorld(localNumber); // Call the method and pass the reference (); } // The instance method is also stored in the method area public void printGreeting() { (greeting + " Number: " + number); } }
Execution process analysis
1. Class loading
When the program starts, the JVM will passClassLoaderloadHelloWorld
bytecode file of class ()arriveMethod areamiddle.
-
Method area (Java 8 and later called Metaspace): Stores information about the class, such as bytecode, static variable, method information of the class (
main
method,printGreeting
Methods, etc.), and constant pools (such as string constant pool storage"Hello, World!"
)。 - Static variables
greeting
Store in the method area,main
The method's bytecode will also be stored in the method area.
start upmain
method
After the JVM is started, it first looks forHelloWorld
Classicmain
Method, executing this method is the entry to the program. A formain
MethodStack Frame, this stack frame will be storedmain
Local variables in the method.
int localNumber = 10;
-
Virtual Machine Stack (JVM Stack):
main
The stack frame of the method is stored in the virtual machine stack. Local variableslocalNumber
The value (10) of the stack frame is also stored.
3. Object allocation
implementHelloWorld hello = new HelloWorld(localNumber);
When the JVM will be inHeap memoryAssign a new one inHelloWorld
Object, store instance variables of this object.
-
Heap memory (Heap):
new HelloWorld(10)
An object instance is created in the heap andnumber
Allocate storage space.number
The value (10) of the value is initialized and stored in the heap. -
hello
The variable itself is aQuote, it is stored inmain
In the stack frame of the method, point to the heapHelloWorld
Object.
4. Method call
When executed();
When the JVM willprintGreeting
Method creates a new stack frame andhello
As implicitthis
Pass to this method.
-
Virtual Machine Stack (JVM Stack):
printGreeting
The stack frame of the method stores local variables and operand stacks, including referencesthis
(Point to theHelloWorld
Object). - exist
printGreeting
The JVM will be retrieved from the heap memorynumber
The value (10) is taken from the string constant pool in the method area"Hello, World!"
, and execute。
5. Output operation
(greeting + " Number: " + number);
This line of code passesCall to output content to the console.
Finally, the console outputs:
Hello, World! Number: 10
-
Method area:
greeting
is a static variable that is already stored in the method area when the class is loaded."Hello, World!"
Stored in a string constant pool. -
Heap memory:
The value (10) of , is obtained from the heap.
6. Garbage Collect (GC)
whenmain
After the method execution is completed,main
The stack frame of the method is destroyed, local variableshello
The quotes will disappear as well. If there is no other place to refer to theHelloWorld
object, then the garbage collector (GC) will think it isUnreachable object, thereby recycling the object at the right time and releasing the heap memory it occupies.
Role summary of memory area
1. Method area (metaspace):
- Stores the metadata, static variables, and constant pool information of the class.
- Static variables
greeting
and the bytecode of the method is stored here.
2. Heap Memory:
- Stores all object instances and arrays.
-
HelloWorld
Object instances are stored in heap memory, instance variablesnumber
It also exists in the heap.
3. Virtual Machine Stack (JVM Stack):
- Each thread will have an independent stack to store the local variables and operand stacks of method calls.
-
main
Methods andprintGreeting
The stack frame of the method is stored in the virtual machine stack, with local variables such aslocalNumber
andhello
References are all in the stack.
4. Program Counter Register:
- Stores the address of the bytecode instruction that the current thread is executing.
- When thread switching, the program counter helps restore the execution location of the code.
5.Native Method Stack:
- Specially used to execute native methods (such as C/C++ code for JNI calls), but not covered in this example.
Summarize
Through this code, the execution logic of the JVM can be clearly displayed:
- The class loader first loads the bytecode into the method area.
- Allocate objects in heap memory, and references are saved in the virtual machine stack.
- The execution process of the method is managed through the creation and destruction of stack frames.
- Eventually, the garbage collector cleans up objects that are no longer used in memory.
This well-defined memory management mechanism allows JVM to run efficiently and ensures memory security and automatic management.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.