Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to warm up java classes to avoid slow first call?

I am doing a project where I need all the API calls to take less than 1s but I am facing an issue with the first call to each route that is slower than the following ones.

Currently the first call to /login takes 3.6s and the next ones take 170ms and same for all the other routes.

I found out using -XX:+TraceClassLoading that on the first call, the classes were loaded in memory and that caused the performance issue.

However I did not find an easy way of loading all the classes at start up and for each new service, I need to add a warm up call in an ApplicationRunner.

Does anyone have a solution to automatically load the classes of a SpringBoot application or warm up all its routes?

like image 604
Ybri Avatar asked Feb 04 '20 22:02

Ybri


People also ask

How do you warm up JVM?

What Is Warming up the JVM. Once class-loading is complete, all important classes (used at the time of process start) are pushed into the JVM cache (native code) – which makes them accessible faster during runtime. Other classes are loaded on a per-request basis.

Why does JVM need to warm up?

Note: Warmup can allow Escape Analysis to kick in and place some objects on the stack. This means such objects don't need to be optimised away. It is better to memory profile your application before optimising your code.


1 Answers

Java's class loading is lazy. This means a class is only loaded by the JVM when it needs to and if it needs to.

If you want to force it to eagerly load classes you just need to reference them. One way of doing it is to iterate through the jar contents or class files to get the class names and then use them to call Class.forName(className).

Additionally, if startup time and performance is very important for your use case, you might want to look into ahead of time compilation solutions like GraalVM, or reduce JIT's threshold for compilation (-XX:CompileThreshold).

like image 157
andresp Avatar answered Sep 26 '22 22:09

andresp