Performance Tuning

Performance tuning is a very complex area, you have to consider many area's network, hardware, application, database, JVM, etc. Performance tuning should not be approached in an ad hoc fashion, performing many changes at once would make it difficult to understand what improved or it could make the system worse. A better approach is an iterative one, an approach were you apply one fix at a time, then test.

  1. Set up a test bed
  2. Do performance testing to obtain a baseline
  3. Investigate bottlenecks within the system these include network, hardware, database, etc
  4. Identify areas where improvements are needed which may or may not include changes to your web app
  5. Make one fix
  6. Repeat steps 2 through 5

The best approach is also not perform the tuning when you are ready to roll out into Production, but try and have an idea at the development stage, software, network and database components can be tested before all the application is put together.

Here are some considerations to think about

Tomcat Error Messages

Here I discuss some common error messages in Tomcat's log files

Tomcat freezes or pauses with no requests being processed Tomcat could be performing alot of garbage collection, use the -verbose:gc flag to see garbage collections, you can adjust parameters such as -Xms and -Xmx.
OutOfMemory exceptions in the Tomcat logs

A memory leak in your application, use a profiler to see if this is the case

The maximum heap memory is less, use the -Xmx option to increase it

Too much memory has been allocated, set your JVM memory close to the systems O/S but leave enough for the O/S

Tomcat catches the JSP content generated, if your JSP pages are very hugh, the result may be OutOfMemory exceptions as the cached response fills up the heap. set the org.apache.jasper.runtime.JspFactoryImpl.USE_POOL and org.apache.jasper.runtime.BodyContentImpl.LIMIT_BUFFER to false to fix this. This would be adjusted in the catalina.bat or file under the CATALINA_OPTS parameter.

The JDK has built-in mechanism to help debug pesky out-of-memory errors, set the parameter -XX:-HeapDumpOnOutOfMemoryError which writes out the heap memory dump when there is an OutOfMemory error

The OutOfMemory error PerGen space errors sometimes occur if you run multiple web applications in a single Tomcat instance or if your application loads lots of classes.Use the parameter -XX:MaxPermSize to increase the Permanent Generation heap space.
Incoming connections take too long or fail (Apache/Tomcat) Look at Tomcats logs for any mod_jk errors, you can increase the maxThreads setting to help

Tuning JVM

JVM has a number of parameters that you can tuning, although short I will add to this list

-Xms The initial heap size for the JVM
-Xmx The maximum heap size for the JVM

Tuning Tomcat

Tomcat has several settings that can affect its performance, also see the HTTP connectors web page.

Tuning the Connector setting (server.xml)
maxThreads using a thread can improve performance, three attributes can control the number of threads, the more important is probably the minSpareThreads making sure there are enough Threads available.
tcpNoDelay when set to true it enables the TCP_NO_DELAY network socket option
maxKeepAliveRequest This attribute controls the keep-alive behavior of HTTP requests, enabling persistent connections, it specifies the maximum number of requests that can be pipelined until the connection is closed by the server
socketBuffer specifies the size in bytes of the buffer to be used for socket output buffering
enableLookups setting this attribute to false disables lookups which can impact performance
Tuning JSP settings (web.xml)
development Setting this to false disables Tomcat from checking the JSP pages for modification
reloading If set to true background compilers are enabled
checkInterval if reloading has been set, this parameter decides how frequently the compiles are triggered
modificationTestInterval This sets the frequency for checking the modification of JSP files.
genStringAsCharArray To generate slightly more efficent char arrays
enablePooling specifies if pooling of Tag library classes is to be enabled
trimSpace remove useless white space bytes from the response
Tuning Host settings (server.xml)
autoDeploy turn off auto deploy in Web applications