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: , , , , , ,
This entry was posted on Sunday, August 15th, 2010 at 6:27 am and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

10 Responses to “HotSpot JVM – Client / Server VM optimization – 260ms vs 0ms!”

  1. Jigga


    What java version do you use?

    I got the same output for both client and server VM.


  2. matto

    Yes, we are using this parameter. Jboss uses this by default, so does IDEA. But this comparison is very nice and shows this difference. I was always wondering what this parameter actually does.

  3. Onkar Joshi

    My testing was with Java 1.6.0_16 – HotSpot build 14.2-b01.

    When you say same output, was it zero in both cases? Or x ms in both cases?

    And was the code *exactly* the same? Small changes can prevent optimization. A faster machine may need a bigger counter. Beware, the int can overflow.

  4. Matteo

    The -server parameter enables a deeper code optimization. For this reason, the bytecode compilation requires more time, but the method execution is far more efficient.

    This is useful for long-running complex applications, like server programs, but it waste lot of effort in desktop applications, which have lots of simple methods.

    Moreover, the -server option is enabled by default in Sun’s 64-bit virtual machines.

  5. Jose María Zaragoza

    About Oracle docs, this feature is from J2SE 5.0 release

  6. Josef B.

    To add to Matteo’s comment, the optimizations also have some repercussions in how the Java Memory Model is used. I read in a Concurrency book that there are subtle issues that may allow a program to run in client mode but ultimately fail in server mode. The recommendation was to run your development environment with the server switch.

  7. Onkar Joshi

    Wow. Which book? Please point me to the part you are talking about. I find it hard to imagine that a program coded correctly and with correct expectations would fail in a different JVM as far as the JVM itself is not buggy.

  8. Installing Tomcat 6 on Windows 7 «

    […] above documentation and information from other resources on the differences between the Client VM and Server VM suggest that we should be using the Server […]

  9. wei

    very good, my testing same as u.
    let me know the -server -client difference

  10. babou

    Interesting post on this subject :

    It says that 64 bit doesn’t do difference between -server and -cleint.

    It also says that in 32bit, server take a long time to optimize appl before lauching.
    That’s means, if you run application with the time command in linux server will take a longer time than client.