May 1, 2013

Java CPU/Memory Heap Usage Monitoring A.K.A. Java Profiling

Java Profiling: One of the important tools needed when working with java is the use of profiling tools to tell you about the memory consumption and CPU usage of each method. VisualVM is a free and easy to use tool. Java profiling will tell you how heap is being used, Garbage Collector operations, threads activities, CPU time of each method, which object is taking most of memory and etc.

To set it up
in ubuntu:

$ sudo apt-get install visualvm
$ jvisualvm

That's it! No set up, parameter passing required whatsoever.! Kudos to VisualVM team. Just run your java program and visual vm will capture the running program and display its monitoring graphs for you.

Here is a video tutorial:

To profile a remote java program:  
Create the file jstatd.all.policy in $JAVA_HOME/bingrant codebase "file:${java.home}/../lib/tools.jar" {permission;}

$ cd $JAVA_HOME/bin; jstatd
$ netstat -nlp | grep jstatd         # make sure jstatd is running
tcp 0 0 :::39779 :::* LISTEN 28661/jstatd
2 tcp 0 0 :::1099 :::* LISTEN 28661/jstatd

The 4th column indicates the ports that are open. In this case, the ports that need to be opened in the firewall are 1099 and 39779.

Open VisualVM, right click on Remote and select Add Remote Host, and finally type the IP address of the remote host.

NOTE: This will only enable CPU sampling for you. To include memory sampling which is very useful: read my response on stackoverflow:   

You can resolved this error by

$ ssh -Y user@remotemachine
$ wget
$ unzip
$ cd visualvm_135/bin
$ ./visualvm

This instance of visualvm will be full featured, including memory and CPU sampling

1 comment:

  1. For me staring visualvm as sudo user solve the issue...