SoFunction
Updated on 2025-04-18

Interpretation of JVM code operation logic

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 (mainmethod,printGreetingMethods, etc.), and constant pools (such as string constant pool storage"Hello, World!")。
  • Static variablesgreetingStore in the method area,mainThe method's bytecode will also be stored in the method area.

start upmain method

After the JVM is started, it first looks forHelloWorldClassicmainMethod, executing this method is the entry to the program. A formainMethodStack Frame, this stack frame will be storedmainLocal variables in the method.

int localNumber = 10;
  • Virtual Machine Stack (JVM Stack)mainThe stack frame of the method is stored in the virtual machine stack. Local variableslocalNumberThe 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 inHelloWorldObject, store instance variables of this object.

  • Heap memory (Heap)new HelloWorld(10)An object instance is created in the heap andnumberAllocate storage space.numberThe value (10) of the value is initialized and stored in the heap.
  • helloThe variable itself is aQuote, it is stored inmainIn the stack frame of the method, point to the heapHelloWorldObject.

4. Method call

When executed();When the JVM willprintGreetingMethod creates a new stack frame andhelloAs implicitthisPass to this method.

  • Virtual Machine Stack (JVM Stack)printGreetingThe stack frame of the method stores local variables and operand stacks, including referencesthis(Point to theHelloWorldObject).
  • existprintGreetingThe JVM will be retrieved from the heap memorynumberThe 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 areagreetingis 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 memoryThe value (10) of , is obtained from the heap.

6. Garbage Collect (GC)

whenmainAfter the method execution is completed,mainThe stack frame of the method is destroyed, local variableshelloThe quotes will disappear as well. If there is no other place to refer to theHelloWorldobject, 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 variablesgreetingand the bytecode of the method is stored here.

2. Heap Memory

  • Stores all object instances and arrays.
  • HelloWorldObject instances are stored in heap memory, instance variablesnumberIt 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.
  • mainMethods andprintGreetingThe stack frame of the method is stored in the virtual machine stack, with local variables such aslocalNumberandhelloReferences 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.