Unknown JVM preheating

1, JVM infrastructure

When the JVM} process starts, ClassLoader will load all required classes into memory, which is mainly divided into the following three steps:

  • Bootstrap Class: the core class library, which is loaded by "Bootstrap Class Loader", such as the basic runtime class library JRE\lib\rt.jar.
  • Extension Class: java. Classes under ext.dirs # path, by ExtClassLoader Responsible for loading. In actual development, if additional class libraries need to be added, they are usually placed in this location.
  • Application Class: the class contained in the actual application, which is determined by AppClassLoader Responsible for loading.

2, What does JVM preheating mean?

After the class loading process is completed, all required classes will enter JVM cache (native code) In this way, it can be accessed quickly in real time. Of course, there are many other classes that have nothing to do with JVM startup that are not loaded at this time.

When the first request of the application arrives, it will trigger the first loading of logic related classes. This process will take a certain amount of time and affect the real-time response of the first call. This is mainly due to the lazy loading and JIT mechanism of the JVM. Therefore, for low latency applications, specific strategies must be adopted to deal with the first preload logic to ensure the rapid response of the first request. This process is called JVM preheating.

3, Tiered Compilation

The JVM puts the frequently used methods into the local cache. To achieve the purpose of rapid call response. Based on this, we can forcibly load our pre recognized high-frequency methods at the beginning of application startup. The setting parameters include the following:

-XX:CompileThreshold -XX:TieredCompilation

Usually, the virtual opportunity collects the method call information fed back to the compiler through the interpreter.

4, Custom implementation

Based on the previous section, we can additionally implement specific logic to make multiple calls to specific methods (- XX:CompileThreshold) to trigger the compilation of the JVM. Examples are as follows:

First, we define a class containing basic methods:

public class Dummy {

    public void m() {

    }
}

 

Secondly, we create a loading class, add static methods inside it, cycle 100000 times, repeatedly generate Dummy objects, and call their methods:

public class ManualClassLoader {

    protected static void load() {
        for (int i = 0; i < 100000; i++) {
            Dummy dummy = new Dummy();
            dummy.m();
        }
    }
}

Now we use the following procedure to test the performance:

public class MainApplication {

    static {
        long start = System.nanoTime();
        ManualClassLoader.load();
        long end = System.nanoTime();
        System.out.println("Warm Up time : " + (end - start));
    }

    public static void main(String[] args) {
        long start = System.nanoTime();
        ManualClassLoader.load();
        long end = System.nanoTime();
        System.out.println("Total time taken : " + (end - start));
    }

}

The test results are as follows:

After preheating

Not preheated

Difference (%)

1220056

8903640

730

1083797

13609530

1256

1026025

9283837

905

1024047

7234871

706

868782

9146180

1053

The performance after preheating is obviously better than that without preheating.

Of course, this is just a simple example test. Specific business logic needs to be considered in practical applications.

5, Tools

It is usually used for benchmarking, and the basic usage is as follows:

Dependent on POM xml:

<dependency>
    <groupId>org.openjdk.jmh</groupId>
    <artifactId>jmh-core</artifactId>
    <version>1.19</version>
</dependency>

<dependency>
    <groupId>org.openjdk.jmh</groupId>
    <artifactId>jmh-generator-annprocess</artifactId>
    <version>1.19</version>
</dependency>

maven library connection: Central Maven Repository.

Define the preheating method and add @ Benchmark annotation:

@Benchmark
public void init() {

    //code todo

}

Put the business logic to be preheated in the preheating processing method.

 

Tags: Java jvm

Posted by dmikester1 on Sat, 16 Apr 2022 21:45:33 +0930