Tips for improving Spring’s start up performance

July 3rd, 2007

Someone 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:

  1. 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).
  2. Reduce the number of configuration files. More configuration == more XML to parse.
  3. 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).
  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.
  5. Reduce your number of beans - just because you can make it a <bean> doesn’t mean you should.
  6. lazy-init=”true” is your friend. Don’t load a <bean> until you need to.
  7. 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!

8 Responses to “Tips for improving Spring’s start up performance”

  1. Bob Lee Says:

    Shortcut:

    1. Use Guice.

    Sorry. Couldn’t resist. ;)

  2. Dan Diephouse Says:

    Shortcut: Bob adds @Resource support to Guice!

  3. Kocka Says:

    I guess these restrictions are really worth doing if the project has some significant performance problem at startup and everything else is fine :)

  4. Ingo Says:

    “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).

  5. hnpacjzfuz Says:

    Hello! Good Site! Thanks you! vsucknyxbh

  6. Jan Hoeve Says:

    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.

  7. Number of Spring configuration files | Bla.es Says:

    [...] 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 [...]

  8. Re: Number of Spring configuration files | Bla.es Says:

    [...] 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. [...]

Leave a Reply