Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot applications consume 100% CPU at startup

Tags:

spring-boot

We have 40+ spring boot apps and when we try to start all of them together parallel, it takes about 9 to 10 minutes. And we notice that CPU usage is always 100% throughout this entire duration. After all apps come up successfully and registered with Eureka, CPU usage is back to normal (on average ~30-40% CPU usage after startup).

It seems each spring boot app is taking at least about 15-20 seconds to startup, which we are not happy with since application is relatively small to start with.

We also disabled spring boot auto-configuration so to make sure only required "matching" classes are loaded at start up by spring boot. And we only gained about 1 or 2 seconds at startup after this change.

We seem to have enough system resources with 8 core CPUs and 32 gb of memory on this VM.

Spring boot version is 1.3.6.RELEASE.

Is it something to do with Spring boot? Because even when we startup single spring boot app it spikes CPU to 70-80% usage. Your help is very much appreciated!

like image 862
Prajesh Avatar asked Nov 13 '17 17:11

Prajesh


People also ask

Why is my CPU at 100% usage all the time?

If the CPU usage is around 100%, this means that your computer is trying to do more work than it has the capacity for. This is usually OK, but it means that programs may slow down a little. Computers tend to use close to 100% of the CPU when they are doing computationally-intensive things like running games.

How do I reduce startup time on spring boot?

spring.main.lazy-initialization=true Setting this property truely will initialize the bean only after it comes into the picture. This property will reduce the run time of the application.

Is it normal for CPU usage to be high on startup?

A spike in CPU use as your computer starts up is not uncommon, but unusually high loads can be caused by too many programs trying to start up at once or an error in your system.


1 Answers

This is more of how many beans and Auto Configurations that get executed while the application being started.

For even a simple web application along with JPA, there is a webcontainer and its thread pools, DataSources initializations and many more supporting beans and auto configurations that need to get initialized. These are some serious resource taking actions and they all are rushed at the start of the application to get application booted as soon as possible.

Given that you are starting 40+ apps like these simultaneously, the server will have to pay its toll.

There are ways you can improve the application boot time.

  1. Remove unnecessary modules and bean definitions from your application. Most common mistake a developer makes is to include a spring-boot-starter-web when the application doesn't even need a web environment. Same goes for other starter modules.
  2. Make use of Conditional Bean definitions with the use of @ConditionalOnMissingBean @ConditionalOnProperty @ConditionalOnClass @ConditionalOnBean @ConditionalOnMissingClass @ConditionalOnExpression. This might backfire if you make spring to check for beans with lots of conditions.
  3. Make use of spring profiles. If you don't want a specific set of beans not to be part of that running instance you can group them into a profile and enable them or disable them
  4. Configure initial number of threads a web container can have. Same goes for Datasources. Initiate your pool with only required number of active threads.
  5. Using lazy-initialization for beans by annotating your classes or beans with @Lazy. This annotation can be per bean or against an entire @Configuration.

If that doesn't satisfy your needs, you can always throttle the CPU usage per process with commands like nice or cputools.

Here is an article around cputools.

like image 84
Raja Anbazhagan Avatar answered Sep 17 '22 06:09

Raja Anbazhagan