HotSpot JVM – Client / Server VM optimization – 260ms vs 0ms!
The Sun/Oracle HotSpot Java Virtual Machine is available for use in two flavors – the Client VM and the Server VM.
The VM to be used can be specified by the -server and -client switches passed when starting the JVM with the java command. The default VM when no switch is specified is the Client VM.
The difference, in a nutshell, is that the Client VM is optimized for fast load times at the cost of runtime performance. The Server VM is built for aggressive optimization for runtime performance at the cost of slower load time.
The example below shows a simple instance of a clearly observable difference between the two. The code puts out the time taken to run a loop that counts down from 500 million to zero.
The results indicate that the Client VM took around 260 milliseconds each time. Amazingly, the Server VM takes ZERO milliseconds each time! This happens because of the more aggressive optimization. The compiler does some funky analysis and reaches the conclusion that the code in the loop does nothing and removing that code would not affect the flow of the program. So it just deletes that code from the compiled code that it produces. This results in the zero run time that we observe. Cool, eh?
Note that there is no need to recompile the code into new .class files between the tests with the two switches. The .class file fed to the JVM is the same in both cases. So the same byte code is initially read but the Server VM puts more effort into doing optimizations and doing JIT (Just In Time) compilation to produce faster code.
Go ahead and check right now if the startup script for your big server side Enterprise Java app has the -server switch. If not, perhaps you should seriously consider adding it in.
Tags: Java, JIT, JVM, Optimization, Performance, Virtual Machine, VM