Saturday, May 13, 2006

Java Hotspot VM : Server Or Client

As a developer, if you have installed a JDK (Java Development Kit)1 downloaded from java.sun.com, you might have noticed that there are two versions of Java Hotspot VM to use. They are JIT (Just-in-Time) compilers for the Java Interpreter to improve the performance of the targeted applications. Broadly there are two types of application - client or server, thus there are two versions of Java Hotspot VM - Client and Server. These two options should not be confused with that the Client VM can communicate with the Server VM. One instance of a Java VM can use2 either a Client or Server Hotspot VM.

The Hotspot (or JIT) compiler technology is the one which boosts the Java performance by interfacing the Java platform and the actual underlying platform. Hotspot provides comparable performance with native applications as the optimization is done on-the-fly, during runtime.

Both are implemented as separate binaries (windows: %JAVA_HOME%\bin\{client|server}; solaris & linux: $JAVA_HOME/lib/$ARCHITECTURE/{client|server}). The Java launcher (${JAVA_HOME}/bin/java[.exe]) chooses the required hotspot VM based on the command line arguments. Though there are two hotspot VMs available, a developer cannot target his Java code for a particular3 VM. Java byte-code is 100% compatible with both the hotspot VMs.
% java -client -version
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)
A Hotspot Client VM is suitable for any short-lived applications like a Java Applet in a web browser or UI application on desktops. Client VM makes the application load faster, does few optimizations compared to Server VM, identifies a hotspot (code that is used heavily by the application which can be optimized) very sooner than the Server VM. Also it uses different set of GC (garbage collection) algorithms than the Server VM. With tiger (1.5.0), Client JVM started sharing the core classes between instances by Class Data Sharing feature. This speeds up the application startup and reduces the memory footprint. The -version output above gives the information whether the JVM is using shared classes sharing. This is not available for Server VM.
% java -server -version
Java HotSpot(TM) Server VM (build 1.5.0_06-b05, mixed mode)
Server VM is best suitable for all long running applications like Application Servers, Web Servers and so. This class VM does more levels optimizations enabled the server applications run faster. With a long running Java process, the amount of garbage created is also very high and proportionate to the heap size (-Xms, -Xmx). With very big heaps, the GC might need more time to clean up. To overcome such issues, Server VM comes with many GC algorithms to choose from. There is also verity of flags and command line options available to fine tune the GC.

There is also an option available to disable both the hotspot VMs. Use -Xint (interpreter mode) . Please note that this also requires any one of the hotspot VM's interpreter, if nothing is specified the default one is used.
% java -Xint -server -version
Java HotSpot(TM) Server VM (build 1.5.0_06-b05, interpreted mode)
Most downloaded configurations of a JRE (Java Runtime Environment) for Windows will have only Client Hotspot VM, unless the platform does not have a client VM.

To change the default VM, move your default choice in the file $JAVA_HOME/lib/$ARCHITECTURE/jvm.cfg to be the first uncommented line.


1. JDK has been known in many forms: JavaTM 2 Platform Standard Edition Development Kit 5.0 with '1.5.0' or 'tiger', J2SE Software Development Kit with '1.4.2' or 'mantis', Java SE Development Kit 6 (the next major release - 'mustang').

2. JVM can also run without any of the Hotspot VM, i.e., it can run as interpreter alone. To do that use -Xint as the command line argument.

3. Due to different optimizations being used by both VMs, reproducing some bug might be different in different hotspot VM. This is purely a runtime issue and not a development time or deployment time issue.

No comments: