Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mvn spring-boot:run slow compared to java -jar xxx.jar

I am trying to migrate an existing command-line app to Spring boot and i have a weird problem.

The app works, but it seems to be very slow when started with mvn spring-boot:run

It is not the app startup that is slow. There is a method which should fetch around 1.8 Mio records from the DB and create POJO's from result set. Normally this takes up to 40 sec.

With app started with maven it takes > 5 minutes.

If i start it with java -jar app.jar it works fine/fast. App is also fast when started in IntelliJ.

I am guessing it may be something with the classpath, but it is just a guess.

All i did in the app is to migrate some Singelton classes to @Components and add spring-boot-maven-plugin

Any ideas ?

like image 879
Georgi Avatar asked Nov 30 '25 11:11

Georgi


1 Answers

The JVM compiles your bytecode for better performance. There are multiple compilers, called C1 and C2.
The C1 compiler is moderately efficient, but compilation is fast.
The C2 compiler is more efficient, compiled code is faster, but this compiler is slower.
So, there is a tradeoff between the performance of the compiled code, and the time needed to compile the code.
If you disable the C2 compiler (by using -XX:TieredStopAtLevel=1, which tells the JVM to use C1 only), your application will start very fast. This is ideal for unit tests, or if your application will not work for a long time, because you won't loose time compiling with C2, and you don't have the time to actually benefit from code compiled by C2. Meanwhile, the code execution speed will not be optimal. This is what happens in your case: your application works over a long period of time, so it could benefit from C2 compilation. Unfortunately, you disabled it, which explains why your custom code is so slow. If you enable C2 compilation, you app will start a little bit slower, but you have time to benefit from C2, and you will obtain better performance over time.

Imagine you want to go to a bakery in order to purchase croissants. 1km. You can take the C1 Lada car: it's slow, but it starts instantly. You can take the C2 Tesla car: it's super fast, but you first have to charge its battery for 5h... You will take the C1 Lada car.
If you want to take a beach vacation 500km away, you will take the C2 Tesla car :)

like image 78
Jonathan Lermitage Avatar answered Dec 04 '25 02:12

Jonathan Lermitage