Jan 23, 2011

Java Verbose Options for Running Programs

With Java, one has so much of power at hand that it is possible to perform any kind of job. You can write to database, file, network or perform complex calculations with ease. Even Oracle is coming up with cloud computing support in Java.

With so much happening in the world of Java, there are many things about which we are ignorant. This article describes the verbose options available with the javac command. I came across these options while I was writing my book on Java Interview Questions. The question was about the importing and loading of classes.

There are basically three sub parameters with the javac verbose parameter:
-verbose:class
-verbose:gc
-verbose:jni


To give you an idea of the number of classes loaded when you fire the java commond, here is the most simple program.

package example.java;

public class Test{
   public static void main(String args[]) {
    System.out.println("Hello World");
   }
}

I will run it using the -verbose:class option to see how many classes have been loaded into the memory. If you are running the program from command line then you can use the command:
java -verbose:class Test

But if you are using an IDE, then follow the screenshots shown below:






The output shown to you will be:

[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.lang.reflect.GenericDeclaration from shared objects file]
[Loaded java.lang.reflect.Type from shared objects file]
.............
.............
[Loaded java.security.ProtectionDomain$2 from C:\Program Files (x86)\Java\jre6\lib\rt.jar]
[Loaded java.security.ProtectionDomain$Key from C:\Program Files (x86)\Java\jre6\lib\rt.jar]
[Loaded java.security.Principal from shared objects file]
[Loaded example.java.Test from file:/C:/Sandeep/Projects/Workspace/test/bin/]
Hello World
[Loaded java.lang.Shutdown from shared objects file]
[Loaded java.lang.Shutdown$Lock from shared objects file]


Thus we can see that such a huge number of classes are loaded for a simple Hello World program. You can imagine the number of classes loaded when you have very complex system to automate with web applications also comming into the picture.


The story of verbose doesn't end here. You can also see when a garbage collection runs. This option can be handy when performing garbage collection is serious to your program. The modified code will be:

package example.java;

public class Test{
   public static void main(String args[]) {
    System.out.println("Hello World");
    System.gc();
   }
}

If you run the above program as java -verbose:gc Test, the you will see the following line in the output:


[Full GC 281K->123K(15872K), 0.0148661 secs]


This can suggest you when the garbage collector runs.

On the similar lines, if you run the above Test program with -verbose:jni option then the following output will be shown:


[Dynamic-linking native method java.lang.Object.registerNatives ... JNI]
[Registering JNI native method java.lang.Object.hashCode]
..................
..................
[Dynamic-linking native method java.lang.Compiler.registerNatives ... JNI]
[Registering JNI native method java.lang.Compiler.compileClass]
[Registering JNI native method java.lang.Compiler.compileClasses]
[Registering JNI native method java.lang.Compiler.command]
[Registering JNI native method java.lang.Compiler.enable]
[Registering JNI native method java.lang.Compiler.disable]
[Dynamic-linking native method java.lang.ClassLoader$NativeLibrary.find ...
[Dynamic-linking native method java.io.FileOutputStream.writeBytes ... JNI]
Hello World
[Dynamic-linking native method java.lang.Runtime.gc ... JNI]
[Dynamic-linking native method java.lang.ref.Finalizer.invokeFinalizeMethod ... JNI]

2 comments: