Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to profile the spring container to learn how to optimize its start up time?

I have an application that is getting fairly large, and the Spring start up time is about 20 seconds. For production use, this is fine, but for development, this is a big pain.

What is a good profiling tool or approach that can give me precisely the information I need to figure out what is taking so long? Maybe it's something I can optimize?

My application is a fairly typical spring/hibernate web app. There's about 50 database tables and several hundred beans (like 200-300... I didn't count). There's a few @Configurable beans. Lots of component scanning. I am also using Spring Security.

I did some primitize profiling with log4j - just at the INFO setting. Here are some of the things that are taking a bit of time:

  • INFO DefaultListableBeanFactory:555 - Pre-instantiating singletons - 2 seconds
  • INFO SessionFactoryImpl:202 - building session factory - 2 seconds
  • INFO HibernateTransactionManager:415 - Using DataSource [com.mchange.v2.c3p0.ComboPooledDataSource......] of Hibernate SessionFactory for HibernateTransactionManager - 7 seconds

There are a couple of things that take .5 to maybe 1 second at the most, but these 3 were the largest ones.

like image 957
egervari Avatar asked Nov 24 '11 07:11

egervari


People also ask

Why does Spring boot take so long to start?

When a Spring Boot Application has slow startup, it can be one or more beans and related dependencies taking longer to initialise and slowing down the entire process. Profiling Spring Boot application doesn't often help in diagnosing the startup issues.

What is special container in Spring boot?

React + Spring Boot Microservices and Spring The Spring container is at the core of the Spring Framework. The container will create the objects, wire them together, configure them, and manage their complete life cycle from creation till destruction.

What is Spring container in Spring boot?

Spring IoC Container is the core of Spring Framework. It creates the objects, configures and assembles their dependencies, manages their entire life cycle. The Container uses Dependency Injection(DI) to manage the components that make up the application.


1 Answers

One option you can try is to use the default-lazy-init option to lazily initialize the beans. Spring initializes all singleton scoped beans during initialization. Read Lazily-instantiated beans section of the reference doc.

Make sure to parameterize the value so that you can change it to true during dev time and false during production deployment.

Component scanning is Slow. Refer to this post on how to disable this when you use Autowiring.

like image 73
Aravind Yarram Avatar answered Nov 13 '22 22:11

Aravind Yarram