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?
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.
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.
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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With