Search code examples
javajvmjitjvm-hotspotwarm-up

How to preload used class when JVM starts?


I am developing a messaging system with Java that requires low latency processing (under 1 second). However, JVM takes warm-up time to process the first input data that causes latency increase (about 2~3 seconds). The main reason of the latency increase was class loading. I know the simplest solution is to use dummy messages to invoke methods in advance. However, I cannot use dummy messages to warm-up JVMs because of the system requirement. So I want to know the method to preload all the used classes when the JVM starts.

I tried the properties to force-load methods by

-XX:CompileThreshold=0 -XX:TieredCompilation

However, it doesn't seem to be working well. The JVM still loads classes when they are called.

I also read other threads, but no one specified the method to preload classes when a JVM starts.

Preloading java classes/libraries at jar startup?

Technique or utility to minimize Java "warm-up" time?


Solution

  • I think you need to be using an ahead-of-time (AOT) Java compiler that compiles classes to native code that can be loaded by the JVM.

    One example is the jaotc tool that was introduced in Java 9 as a result of the JEP 295 work. It takes a list of classes (previously compiled to bytecode files) and compiles them to a native code library; e.g. a Linux .so file. You can then tell the JVM about the .so file on the command line, and it will load the AOT compiled code and use it.

    The upside of AOT compilation is faster JVM startup. The downside is that an AOT compiler can't do as good a job of optimizing as a JIT compiler, so overall performance suffers in a long running program.

    So your (apparent) need to meet a fast startup requirement may lead to you not meeting a long-term throughput requirement.

    Additional references: