Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Rework. Add introduction. Describe options to counter annotation scanning etc.

How do I make Tomcat startup faster?

Jars

...

This section provides several recommendations on how to make your web application and Tomcat as a whole to start up faster. Before we continue to specific tips and tricks, the general advice is that if Tomcat hangs or is not responsive you have to perform diagnostics. That is to take several thread dumps to see what Tomcat is really doing. See Troubleshooting and Diagnostics page for details.

The Servlet 3.0 specification introduces support for

  • javax.servlet.ServletContainerInitializer (shortened as SCI)
  • Web fragments (META-INF/web-fragment.xml)
  • Using annotations to define components of a web application (Servlets etc.)
  • Using annotations to define components processed by an SCI (@HandlesTypes annotation on a SCI)
  • Packing web application resources in jar files (META-INF/resources/*)

These features are collectively referred as "plugability features" and are there to simplify plugging of additional frameworks into a web application. See chapter 8 of Servlet 3.0 specification for details.

These features require scanning the JAR files. The worst is scanning for annotated classes. There are a lot of class files to process and parsing a class file takes noticeable time.

It is possible to configure a web application to omit most of the scanning. It is also possible to configure which JARs Tomcat should skip in its scanning.

Other features that require scanning are:

  • Discovery of tag libraries (META-INF/**/*.tld) (shortened as TLD scanning)

The TLD scanning happens when Tomcat needs to compile a JSP page. It does not affect the startup time, but does affect the access time for the first not-yet-compiled JSP page in the web application.

Remove unnecessary JARs

Remove any JAR files you do not need. When searching for classes every JAR file needs to be examined to find the needed class

...

. If the jar file is not there - there is nothing to search.

...

Memory

...

Config

  1. Trim the config files as much as possible. XML parsing is not cheap. The less there is to parse - the faster things will go.

Webapp

  1. Make sure your code is not doing slow things. (Use a profiler) 2. Remove any webapps you don't need. (So remove the all the webapps installed with tomcat) 3. Add metadata-complete="true" attribute to the <web-app> element of your WEB-INF/web.xml file, if you do not need the features that are enabled by the default value of "false" of that attribute. This way Tomcat would not waste time scanning the libraries to autodiscover annotated classes and web fragments.

Starting several web applications in parallel

...

Note that a web application should never have its own copy of Servlet API or Tomcat classes. All those are provided by the container (Tomcat) and should never be present in the web application. If you are using Apache Maven, such dependencies should be configured with <scope>provided</scope>. See also a stackoverflow page.

Configure your web application

There are two options that can be specified in your WEB-INF/web.xml file:

  1. metadata-complete="true" attribute on the <web-app> element. 2. an empty <absolute-ordering /> element.

"metadata-complete="true"" disables scanning your web application and its libraries for classes that use annotations to define components of a web application (Servlets etc.). This option is not enough to disable all of annotation scanning. If there is a SCI with a @HandlesTypes annotation, Tomcat has to scan your application for classes that use annotations or interfaces specified in that @HandlesTypes annotation and provide the list of such classes to the SCI.

An example of such SCI is in the WebSocket API implementation jar (tomcat-websocket.jar, tomcat7-websocket.jar) which is included with Tomcat 7 starting with 7.0.47.

The "<absolute-ordering>" element specifies which web fragment JARs (according to the names in their WEB-INF/web-fragment.xml files) have to be scanned for SCIs, fragments and annotations. If you specify an empty "<absolute-ordering/>" it means that none of that scanning will occur. This feature does not affect scanning for web application resources.

Exclude JARs from scanning

In Tomcat 7 JAR files can be excluded from scanning by listing their names or name patterns in a system property. Those are usually configured in the conf/catalina.properties file.

In Tomcat 8 you can use a system property or configure a <JanScanFilter> element in the context file of your web application.

Entropy Source

Tomcat 7+ heavily relies on SecureRandom class to provide random values for its session ids and in other places. Depending on your JRE it can cause delays during startup if entropy source that is used to initialize SecureRandom is short of entropy. You will see warning in the logs when this happens, e.g.:

No Format
<DATE> org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [5172] milliseconds.

...

Note the "/./" characters in the value. They are needed to work around known Oracle JRE bug #6202721.

Also note that replacing the blocking entropy source (/dev/random) with a non-blocking one actually reduces security because you are getting less-random data. If you have a problem generating entropy on your server (which is common), consider looking into entropy-generating hardware products such as "EntropyKey".

Starting several web applications in parallel

With Tomcat 7.0.23+ you can configure it to start several web applications in parallel. This is disabled by default but can be enabled by setting the startStopThreads attribute of a Host to a value greater than one.

Memory

Tweak memory parameters - Google is your friend.

Config

Trim the config files as much as possible. XML parsing is not cheap. The less there is to parse - the faster things will go.

Webapp

  1. Remove any webapps you don't need. (So remove the all the webapps installed with tomcat) 2. Make sure your code is not doing slow things. (Use a profiler)

...

CategoryFAQ