1

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?

mori
  • 13
  • 2
  • 2
    Don’t mix up “loading”, “initializing”, “compiling”, and “optimizing”. Even ahead-of-time optimization may lead to code that does not meet the actual requirements when done without knowledge about the actual behavior. So when you want to avoid de-optimization and re-optimization and compilation, you should consider dummy messages that force the application to take the relevant paths in a typical way. “because of the system requirement” is not a convincing reasoning not to do it. If your system has no testing functionality, there’s something wrong with your system. – Holger Oct 26 '20 at 11:00
  • Thank you for comments. What I want to do is to load classes before execution. The reason why I referred to compile options was that I read a blog (https://www.baeldung.com/java-jvm-warmup) that said the compiling options led force class loading. I understand using dummy messages is a correct way, but I want to know whether there are other options. – mori Oct 28 '20 at 05:33
  • I can't make much sense of what that page is saying. – Stephen C Oct 28 '20 at 06:53
  • 1
    That’s becoming a pattern. Every time, I see a `baeldung.com` link in a stackoverflow question, I’m already expecting a sloppy article creating more confusion than clarification. Point 3 links to an IBM WebSphere servlet output cache that has nothing to do with JVM code caching. Besides that, it lists some platitudes without any worth. When you add a warmup operation, but exclude it from the measuring, the measured time becomes shorter, obviously, while the overall startup time became longer. But nowhere does it say that the compilation forces loading. It’s the first time (“warmup”) execution. – Holger Oct 28 '20 at 08:26

1 Answers1

1

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:

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1
    That is a great way to meet the demand. Thank you! But, I still want to know if there is another way to load classes before execution, for example, specifying some options. – mori Oct 28 '20 at 05:35
  • 1
    There are no options to do that. Besides, the problem is probably not loading per se. It is more likely the time taken to 1) gather profiling information and 2) run the JIT compiler. Plus, there are other warmup effects due to initial heap expansions. – Stephen C Oct 28 '20 at 06:49