Research on JVM principle


1. Location of JVM

The JVM runs on the operating system.

Research on JVM principle


2. Architecture of JVM

Research on JVM principle

  • firstgarbage collectionDefinitely notStack, local method stack, program counterIn, the system will automatically release the stack after it runs out
  • So calledJVM tuning, 99% is actuallyReactor and method areaAnd 99% of them are in tuneheap, we say that the method is actually a special heap

3. Class loader

The function of class loader: load class files

For example, we have a student entity class

When we execute new student(); After that, it is explained that the abstract student class becomes a concrete instance. The reference will be put on the stack and the specific object will be put on the heap.

Research on JVM principle


The above figure illustrates the process of object instantiation, which is to explain the new keyword and class keyword. We say that class is a template and object is a concrete instantiation.

package com.wangcp.jvmstudy.study01;

public class Car {
    public static void main(String[] args) {
        //A class is a template and an object is a concrete instance
        Car car1 = new Car();
        Car car2 = new Car();
        Car car3 = new Car();
        System. out. Println ("------------------ Car object instance ------------");

        Class<? extends Car> aClass1 = car1.getClass();
        Class<? extends Car> aClass2 = car2.getClass();
        Class<? extends Car> aClass3 = car3.getClass();
        System.out.println("-----------------Car Class--------------");

The result is:

-----------------Car object instance--------------
-----------------Car Class--------------

From the execution results of the above code, we can see that the three car objects from new are different, but the class template is the same.

4. Parental appointment mechanism

The main purpose of the parent delegation mechanism is for program security. For example, we create a new package Java Lang and a new string class is created under the package and a toString () method is added, but it will be found that it cannot be used and an exception will be thrown when actually calling. Because when loading a class, it will delegate upward until the root loader checks whether the current class can be loaded. When a string class is found in the root class, it will be loaded directly and will not be called downward.

Steps of parent delegation mechanism:

  • 1. The class loader receives the request for class loading

  • 2. Delegate the request upward to the parent class loader to complete it until the class (root) loader is started

  • 3. Start the class loader to check whether the current class can be loaded. If it can be loaded, it will end and use the current loader. Otherwise, throw an exception and notify the child loader to load.

  • 4. Repeat step 3
    If this type is not found in the above steps, an error will be reported. We often see:

Class Not Found~


native: for those with native keyword, it means that the scope of action of Java cannot be reached, and they will call the library of the underlying C language!

For example, when we execute the thread, we can see that the source code of start () is:

public synchronized void start() {
         * This method is not invoked for the main method thread or "system"
         * group threads created/set up by the VM. Any new functionality added
         * to this method in the future may have to also be added to the VM.
         * A zero status value corresponds to state "NEW".
    if (threadStatus != 0)
        throw new IllegalThreadStateException();

    /* Notify the group that this thread is about to be started
         * so that it can be added to the group's list of threads
         * and the group's unstarted count can be decremented. */

    boolean started = false;
    try {
        started = true;
    } finally {
        try {
            if (!started) {
        } catch (Throwable ignore) {
            /* do nothing. If start0 threw a Throwable then
                  it will be passed up the call stack */

Let’s focus on the start0 () called in this method; Method. The source code of this method is:

private native void start0();

This is a local method, which is above usJVM architecture diagramCan be seen inNative method stack

This is the JNI (Java Native Interface) specifically used to call local methods.

JNI action: expand the use of Java and integrate different programming languages for java! At first, I wanted to integrate: C, C++

Java inMemory areaThe technical secondary school has opened up a marking area:Native Method Stack, register the native method

In the final execution, the methods in the local method library are loaded through JNI

Native is generally not used in our daily development. It can only be used when operating hardware, such as Java program driven printer, management system, etc

6. PC register

Program counter: program counter register

Each thread has a program counter. When the thread is private, it is a pointer to the method bytecode in the method area (used to store the address of an instruction and the instruction code to be executed). When the execution engine reads the next instruction, it is a very small memory space, which can be almost ignored.

7. Method area

Method area

The method area is shared by all threads, and all fields and method bytecodes, as well as some special methods, such as constructors and interface codes, are also defined here,

In short, the information of all defined methods is saved in this area,This area belongs to the shared section;

Static variables, constants, class information (construction methods, interface definitions) and runtime constant pool exist in the method area, but instance variables exist in heap memory, which has nothing to do with the method.

8. Stack

Stack:Also calledStack memory, in charge of program running, life cycle and thread synchronization; When the thread ends, the stack memory is released. For the stack, there is no garbage collection problem.

Stack also represents a data structure,First in first out, last in first out。 The corresponding is the queue, first in, first out.

What is stored in stack memory?

Methods of 8 basic types, object references and instances

9. Pile

Heap, a JVM has only one heap memory, and the size of the heap memory can be adjusted.

After the class loader reads the class file, what will be put into the heap? Classes, methods, constants, variables… Store all of our reference typesReal object

The heap memory is further subdivided into three areas:

  • Xinsheng district (Eden Park)
  • Old age area
  • Permanent area perm
Research on JVM principle


GC garbage recycling is mainly in Eden Park and the elderly area.

Suppose the memory is full, oom, and the heap memory is not enough! Java. lang.OutOfMemoryError:Java Heap space

After jdk8, the permanent storage area is renamed meta space.

Keep learning, update and make progress every day!