Java memory profiling questions and asnwers
The questions and answers on Java performance and CPU profiling was discussed in a separate blog. This blog covers memory profiling. Try this blog entry if you want to know how to create memory leak in Java.
Q. How will you go about profiling your Java application for memory usage?
A. There are number of tools both commercial and open-source. There are command line tools that get shipped with Java like hprof, jconsole, jhat, and jmap. Here is an example with hprof.
java -agentlib:hprof=heap=all,thread=y,format=b test.ProfilingTestWhen you run the above command in binary format (i.e. format=b), it produces a heap dump file called “java.hprof” when the program exits or a control character (Ctrl-\ or Ctrl-Break on WIN32 and QUIT signal is received kill -QUIT on Unix machines) is pressed, which can be opened in eclipse memory analysis tool (MAT) for further analysis and leak detection.
A Java heap dump is an image of the complete Java object graph at a certain point in time. It includes all objects, fields, primitive types and object references. It is possible to instruct the JVM to create a heap dump automatically in case of a OutOfMemoryError with the JVM option -XX:+HeapDumpOnOutOfMemoryError. You could also dump the heap on ctrl+brk key stroke by setting the JVM argument -XX:+HeapDumpOnCtrlBreak.
The above heap dump can also be analyzed instantly with the jhat tool that is shipped with your java.
jhat java.hprof
The above command starts a web server as shown below
Reading from java.hprof... Dump file created Mon Nov 21 12:52:22 EST 2011 Snapshot read, resolving... Resolving 5426 objects... Chasing references, expect 1 dots. Eliminating duplicate references. Snapshot resolved. Started HTTP server on port 7000 Server is ready.
The report can be viewed via an internet browser using http://localhost:7000
The “jmap” and jconsole are handy tools for analyzing heaps and garbage collection respectively. If you start your application with the following command line options, you will have a number of options to gather profiling data in Java.
java -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9000 \ -Dcom.sun.management.jmxremote.authenticate=false \ -Dcom.sun.management.jmxremote.ssl=false \ -agentlib:hprof=heap=dump, format=b,depth=10 test.ProfilingTest
If you type jconsole on a command line, you will get the following console.
The “4564” is the process id of the Java process. You can also find out the process id by typing “jps” on a command line.
Click on the connect button to view the different profiles as shown below. The jconsole has a button to invoke the garbage collection at your will along with other useful profiling information.
The “jmap” is a handy utility that will cause the heap dump of a running Java application. Find out the process id <pid> with the following command
jps
and then run the jmap command as shown below with the relevant
jmap -dump:format=b,file=hprof.bin 4564
The heap dump file hprof.bin can be opened with jhat and eclipse memory analysis tool for further analysis.
There are commercial tools that can be used in production environment like YourKit for Java & .Net, JProfiler for Java, etc to tools for larger distributed and clustered systems with large number of nodes like CA Wiley Introscope for Java and .Net, SiteScope from HP and ClearStone for Java.
Labels: Java Key Areas
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home