Tracking Class Loading and Garbage Collection
by, 11-08-2011 at 06:41 PM (2129 Views)
When any Java program runs, In background some libraries are loaded by JVM (Java Virtual Machine) in order to execute the Java program. Java command line tool(java) has various options that might be helpful for a Java programmer to know more about the class loading mechanisms.
For example cp option that instructs JVM to find necessary classes other than system classes. An usage is shown below
java -cp ExampleDir.ExampleTipIf ExampleTip.class is not found in ExampleDir directory, Then this command will give NoClassDefFound Error.
There are some command line features available for class loading and garbage collection that are not used very frequently. Some are listed below.
-verbose:class option :- It reports when a class is loaded into the JVM and from where it came while running the java program. Suppose a user runs a swing application from the command prompt packaged in a jar file using this option then you will get a report in the command window about the classes loaded.
For eg. There is a notepad application that comes with JDK. It is located at demo\jfc\Notepad directory of JDK.Run the application using following command.g=
C:\ java -verbose:class -jar "c:
Program FilesJavajdk1.5.0_07demojfcNotepadNotepad.jar"You will see names of classes being loaded on command prompt.
[Opened C:Program FilesJavajdk1.5.0_07jrelibrt.jar]
[Opened C:Program FilesJavajdk1.5.0_07jrelibjsse.jar]
[Opened C:Program FilesJavajdk1.5.0_07jrelibjce.jar]
[Opened C:Program FilesJavajdk1.5.0_07jrelibcharsets.jar]
[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.lang.reflect.AnnotatedElement from shared objects file]
[Loaded java.lang.Class from shared objects file]
[Loaded java.lang.Cloneable from shared objects file]
[Loaded java.lang.ClassLoader from shared objects file]
[Loaded java.lang.System from shared objects file]
[Loaded java.lang.Throwable from shared objects file]
The first four lines indicates that some jar files being opened from system library . Then in the next lines you can see names of classes being loaded to run the program. Different implementations of the Java platform can have different formats here. The only thing is that -verbose:class displays messages as classes get loaded and unloaded. Java program will run automatically when all classes have been loaded. While running the application if some event is fired and some extra classes are loaded or unloaded, those will be also shown in the commad window.
To know about each garbage collector use -verbose: gc while running java program. It displays time for garbage collection to run, and the
before and after heap sizes.
Following is an example for the above mentioned application.
C:\java -verbose:gc -jar "c:Program Files\Javajdk1.5.0_07\demojfc\Notepad\Notepad.jar"[GC 512K->230K(1984K), 0.0045783 secs]
[GC 742K->398K(1984K), 0.0055068 secs]
[GC 910K->472K(1984K), 0.0046811 secs]Application will be launched and on each garbage collector call JVM will display a similar message like the above. It is very important that this information can be useful in debugging. Like it could help you determine if garbage collection happened at a critical point of time that might caused a program to crash. It is very much useful while mixing Java and C/C++ code with JNI, especially when there is an underlying bug on the C/C++ code side.