JVM Server vs Client Mode
JVM is launched in client mode by default in SUN/Orace JDK. JVM provides option to launch it in server or client mode. These two modes give different run time performance options.
At high level there are two steps from java source to running program. In first step we compile the java source to binary class file. In next step binary class is executed. In java class execution, combination of interpretation and compilation is done. Generally it is interpretation and to boost the performance JVM uses a just-in-time (JIT) compiler.
JIT compiles selective block of code to native instructions. Because running native instructions is always better and gives good performance. This is done at the cost of spending resource to compile code to native. There will be a performance benefit, if that block of code is used repeatedly as in subsequent runs instead of interpreting the binary code the native instructions will be executed.
When the JVM is launched in ‘server’ mode, it performs aggressive optimization than the ‘client’ mode. So server mode is better suited for production deployments and gives better performance. In case of tiny programs, where there is minimal scope for optimization server mode may not be best suited and in times it can even give worst performance than the client mode because of the additional cost of native code conversion.
The policy of having a JIT compiler and the performance optimization algorithm are completely the choice of the JVM implementors and it is not enforced in JVM specification. So when we study this behavior it is completely specific to the JVM we use and it cannot be generalized.
How to choose between client and server jvm mode?
To choose between server and client mode we can have client mode for small programs that are not executed for a longer period and server mode otherwise. The startup of the JVM might be slower in server mode and the runtime memory footprint also will be larger. Client mode starts JVM in quicker time and memory footprint is also lesser. This is because client mode does not try to optimize many code blocks as the server mode does. Therefore the shorter startup time and less memory usage.
So what is the special optimization done by server mode? One example is in lining of virtual method invocations wherever it is used. This is done by adaptive compilation. One more thing done by server mode is, it does not give back the memory acquired back to the OS till the execution is complete. But in case of client mode if a certain block of memory is left unused it may give back to the OS during the execution of the program. Initial options like InitialHeapSize and MaxHeapSize are taken as large numbers in server mode on launch in comparison with client mode.
JVM hotspot VM options provide rich set of opportunities to calibrate the runtime performance. So to launch is client mode we need not give any options as by default the JVM launches in client mode. To launch in server mode just use “- server” when we run java tool like,
java -server ClassName
This is based on SUN/Oracle JDK. If it is JRockit VM the default is server mode and client mode should be launched using option “-client”. Ah I forgot to mention a thing, as always the same java class can be used for running in both the modes. When we download JDK we get both the modes bundled. If we download JRE alone, then we get only the client mode with it.
Just in case if you don’t know what JVM you are using then the following java code should help.
String jvmName = System.getProperty("java.vm.name"); // jvmName = Java HotSpot(TM) Server VM
In a future article, I plan to showcase performance benchmark of sever and client mode.