How to tune your JVM instance

Last couple of days I’ve been busy tuning a Sun JVM. Although there is a lot to find out there on the internet, there are no golden rules on what the perfect setting is. Tuning a JVM is an on-going process and a cooperation between an administrator and a developer. The administrator has to monitor closely what resource he gives to the JVM, while the developer knows how the application works internally and if some settings are not in conflict of the applications operation.

First thing, you need tools to monitor en test you application. Examples are jVisualVM, which comes whit the Sun JDK, for monitoring the memory usage and jMeter or Oracle Real Application Testing for stress testing the J2EE application. Please notice that it is a good thing to install the extra plug-ins on VisualVM, especially the VisualGC and Mbeans plug-in are very handy. VisualGC give you a good look on how the garbage collector and the occupation of the different memory spaces are behaving.

Next comes the configuration of the different memory segments,  the heap space, which contains the young and old generation,  and the permanent generation. A good thing to know for any administrator, the young generation contains three spaces, eden and survivor space 1 and 2. The behavior of these different memory segments can be extensively documented but that is not the scope of this blog. In sort: object live in the eden space and when they are still needed they survive a garbage collect and move to the survivor space, when thous objects are still needed on the next GC run they move to SS2, after that they go to the old generation. That is globally it. By choosing you memory settings carefully you can influence when a object move to the next space and how many.

One thing that must not be over looked are time-outs on the several type of objects and sessions. For example: when the web session time-out is set to high, http-sessions remain unneeded active and consumes heap space. When setting the time-out on your JVM right for your application, it can boost and stabilize your application a great deal.

Although there are numerous settings for tuning a JVM; type of garbage collector, memory sizes, maximum memory settings, it all comes down to one thing. How important is performance for your application, maybe the default settings are good enough. Questions your self this, then choose the garbage collector type and appropriate memory settings. In experience a good start is to choose  fixed sizes for you memory segments and a low impact garbage collector and start from there. For example:

  • Xmx512m; total heap space
  • Xms256m; initial heap space on startup
  • Xmn64m; young generation size
  • -XX:SurvivorRatio=16; relative size of SS1 and SS2 to the eden space
  • -XX:+UseConcMarkSweepGC; Low Pause Concurrent Mark Garbage Collector (low CPU cost)
  • -XX:MaxPermSize=128m; Permanent space size, for loading libraries needed for your application.

I hoop this give you a heads up on tuning a JVM. In my experience, a good knowledge on the inner workings of a JVM gives you a good idea on how a J2EE applications server works. Whether it is Weblogic, Glassfish or jBoss.