WebReference.com - Chapter 16 of Java Servlets Developer's Guide, from Osborne/McGraw-Hill (4/4)
Java Servlets Developer's Guide
Tune Your Servlet Container
The servlet container you are using may also provide you with opportunities for optimization. Since there are numerous servlet containers, and they are all quite different in the way they are managed and configured, I will simply provide you with some general suggestions of areas that you might be able to tune:
- Thread usage Many servlet containers allow you to tune the number of threads that the container will use to serve requests. Increasing the number of threads may improve overall performance when the container is under load. Beware, however, that simply setting the thread count higher may result in system resources being used unnecessarily.
- File read cache size Some servlet container may maintain an internal cache to hold the contents of static files. Increasing the size of the cache may result in more files being retained in memory at the expense of more system memory being used.
- Disable logging Once you have your applications up and running, you may want to turn off any verbose (or debug) logging in your servlet container.
- Disable servlet reloading Many servlet containers support automatic reloading of servlet class files, allowing you to simply recompile a servlet and have it automatically reload. This turns out to be an expensive trick, because the servlet container must check the last-modified date of the servlet class file with each request. In production systems, you will most likely want to disable this feature.
In addition to these general suggestions I would advise you to read the documentation for the servlet container you are using for other tuning parameters.
Tune Your Virtual Machine
Perhaps one of the easiest ways to boost the performance of your servlet container is to tune the Virtual Machine. In most cases, you can increase the initial size of the heap. But be careful: Using a larger heap size decreases the frequency of garbage collection by the VM, but increases the time that the garbage collector takes when it does run. The heap size is controlled by the Âmx option, but you should check the documentation for the particular VM you are using before attempting to modify the size of the heap.
In this chapter, we have focused on ways to ensure that your servlet performance is at a level that keeps your users happy. There are lots of common performance pitfalls when using Java, such as String concatenation or synchronization, that can have a negative impact on your application. Simply being aware of these pitfalls, and how to work around them, can help you to write efficient servlets.
Once you have written your servlets, there are also ways to boost performance by tuning the servlet container you are using, as well as the underlying Virtual Machine.
Created: July 31, 2002
Revised: July 31, 2002