Tips for improving Spring’s start up performance
July 3rd, 2007Someone is blathering on again about how Spring sucks, and so startup performance came up in the conversation. CXF uses Spring under the covers by default (it’s optional, don’t worry). Initially there was some major slowness, but I’ve spent a lot of time profiling/improving CXF’s startup time. I thought I’d share what I learned so you can improve your startup time as well:
- Use the latest version of Spring (2.0.5+). The latest version of Spring contains may performance improvements (including one I found while profiling CXF).
- Reduce the number of configuration files. More configuration == more XML to parse.
- Reduce the number of <bean>s in your configuration. Contrary to popular belief, not every friggin class needs to be a <bean>. Startup time is pretty proportional to the number of beans (if you follow the suggestion in #4).
- Don’t use classpath*:*.xml type wildcards. These significantly slow down the starup process as Spring needs to search through all the jars. In some cases it needs to expand them (I think on certain appservers). Doing a classpath:/foo/*.xml search is much better as that can delegate to the JDK. Although further performance gains can be made by removing wildcards together I think.
- Reduce your number of beans – just because you can make it a <bean> doesn’t mean you should.
- lazy-init=”true” is your friend. Don’t load a <bean> until you need to.
- Turn off schema validation (Alternately: anyone want to rewrite Xerces?) – this adds about 20% more time to startup in the CXF case.
If all goes well, you should be able to get the Spring startup time pretty low. At this point its probably other things, like your ORM layer (*cough* Hibernate *cough*), that are slowing things down.
Random tidbit: I was profiling the CXF startup the other day. About 33% of the time is in Introspector.getBeanInfo(), so its really not all Spring’s fault IMO
. Maybe someone can hack the JDK to be faster there?
Have any more Spring performance tips? Add them to the comments!
July 3rd, 2007 at 9:54 am
Shortcut:
1. Use Guice.
Sorry. Couldn’t resist.
July 4th, 2007 at 12:07 am
Shortcut: Bob adds @Resource support to Guice!
July 9th, 2007 at 7:29 pm
I guess these restrictions are really worth doing if the project has some significant performance problem at startup and everything else is fine
July 23rd, 2007 at 6:34 am
“lazy-init=â€?trueâ€? is your friend. Don’t load a until you need to.”
Depends on your needs. I generally prefer loosing a few extra seconds at startup rather than at runtime (even if only once).
July 30th, 2007 at 12:54 pm
Hello! Good Site! Thanks you! vsucknyxbh
August 28th, 2007 at 2:21 pm
Hi, I created a blog on number 6: lazy-init.
I describe a method I found to programmatically set beans to behave lazy.
See: http://janhoeve.blogspot.com/2007/08/speedup-development-by-making-spring.html
This method is not usefull in production (remember the impatient enduser), but speeds up normal development.
At my projects, 25% of the deploy time is wasted by the preInstantiation of spring.
September 23rd, 2007 at 10:11 am
[...] agree with this. Also see netzooid.com/blog/2007/07/03/tips-for-improving-springs-start-up-performance/: Having a lot of XML files means a lot of files to parse which slows down the [...]
September 23rd, 2007 at 10:11 am
[...] one >> file. Otherwise not. What is your opinion? > > I agree with this. Also see > netzooid.com/blog/2007/07/03/tips-for-improving-springs-start-up-performance/: > Having a lot of XML files means a lot of files to parse which slows down > the startup. [...]