Monday, August 18, 2014

Java Virtual Machine (JVM)

JVM stands for Java Virtual Machine. It is the runtime platform which gives life to our programs. The JVM contains a JIT compiler and Garbage collector.
Java VM comes in both 32 bit and 64 bit versions. With 32 bit version, there is a Java heap limitation that can be handled in various operating systems. The range goes from 1.5G in windows to around 2.5 Gig in different flavors of kernel. With 64 bit, the Java heap size that can be handled becomes larger but comes with a penalty of large object pointers which go from 32 bit to 64 bit. This introduced a performance penalty of around 10%.. There is a setting whcih can be passed as runtime argument, can compress the pointers in 64 bit architecture. The setting is
-XX:+UseCompressedOops
The java virtual machine can be started in two modes.
client - Typically used for desktop application. The java is started with -client option
server - Typically used for server application which are not interactive in nature. The java is started with -server option
Let's write simple HelloWorld class and run it with two options:
public class HelloWorld {
public static void main(String[] args) {
            System.out.println("Hello World");
}
}
Now run the code with java  client option
java -client -verbose
[Loaded java.lang.Object from shared objects file]
[Loaded java.io.Serializable from shared objects file]
[Loaded java.lang.Comparable from shared objects file]
[Loaded java.lang.CharSequence from shared objects file]
[Loaded java.lang.String from shared objects file]
....
[Loaded java.security.BasicPermissionCollection from shared objects file]
[Loaded java.security.Principal from shared objects file]
[Loaded java.security.cert.Certificate from shared objects file]
[Loaded HelloWorld from file:/home/lalit/Data/crayom/training/Performance/eclipseCode/HelloWorld/bin/]
Hello World
[Loaded java.lang.Shutdown from shared objects file]
[Loaded java.lang.Shutdown$Lock from shared objects file]
With server option
java -server -verbose
[Opened /usr/lib/jvm/java-6-sun-1.6.0.16/jre/lib/rt.jar]
[Loaded java.lang.Object from /usr/lib/jvm/java-6-sun-1.6.0.16/jre/lib/rt.jar]
[Loaded java.io.Serializable from /usr/lib/jvm/java-6-sun-1.6.0.16/jre/lib/rt.jar]
[Loaded java.lang.Comparable from /usr/lib/jvm/java-6-sun-1.6.0.16/jre/lib/rt.jar]
[Loaded java.lang.CharSequence from /usr/lib/jvm/java-6-sun-1.6.0.16/jre/lib/rt.jar]
[Loaded java.lang.String from /usr/lib/jvm/java-6-sun-1.6.0.16/jre/lib/rt.jar]
...
[Loaded java.security.BasicPermissionCollection from /usr/lib/jvm/java-6-sun-1.6.0.16/jre/lib/rt.jar]
[Loaded java.security.Principal from /usr/lib/jvm/java-6-sun-1.6.0.16/jre/lib/rt.jar]
[Loaded java.security.cert.Certificate from /usr/lib/jvm/java-6-sun-1.6.0.16/jre/lib/rt.jar]
[Loaded HelloWorld from file:/home/lalit/Data/crayom/training/Performance/eclipseCode/HelloWorld/bin/]
Hello World
[Loaded java.lang.Shutdown from /usr/lib/jvm/java-6-sun-1.6.0.16/jre/lib/rt.jar]
[Loaded java.lang.Shutdown$Lock from /usr/lib/jvm/java-6-sun-1.6.0.16/jre/lib/rt.jar]
It's quite a verbose output, however for clarity most of the output is removed. The only noticeable difference here is the location from where the objects are loaded. The client and server vm both use the same code base. However the client VM is designed for fast load up time and small foorprint. On the other hand, server VM is designed maximum performance speed and involves adaptive optimization techniques not present in client VM. Also server VM are tuned to run for longer durations.
Starting from Java 5 the definition of server machine is:
>=2 Physical processors
>= 2GB of physical memory
The detail classification of server and client machine can be accessed at here
The VM by default runs in the server or client mode based on the machine classification. However the setting can be changed by providing the switches specifically while starting java.

No comments:

Post a Comment