Notes on Performance Tuning of Java

When you need to analyze performance of a running Java program, there is a simple profiling tool already built in to the Java runtime environment: HPROF  It allows you to analyze both execution time and heap allocation. It’s really basic, but gets the job done — and it’s free and easy to use.

For analysis of the Java heap, add the following option to your Java command line.


Other options are “heap=dump” and “heap=all”.

Run Java, and then when your program completes, look for the “java.hprof.txt” inside the folder you started Java. The contents of that file depends on which option you choose. The “sites” option is particularly useful when you want to look for a memory leak. Run your program, ending with a System.gc (). Then look in the output to see if there are unexpected objects still being held onto. It’s also useful if you think you are generating lots of temporary objects and spending a lot of time in garbage collection.

For analysis of cpu time, there are HPROF options to sample cpu time and to do more precise timing. It is best to start with cpu samples because there is not much overhead to running  your program that way.



Information about the HPROF profiler can be found at the following locations:

Garbage Collection Information

If you want to study garbage collection performance, there are more options you can add to your Java command line:

-verbose:gc -Xloggc:gc.out -XX:+PrintGCDetails

See also:

About Bill Lahti

Bill Lahti is a software engineer building mobile applications and knowledge management solutions. Two of his interests are writing for this blog and building Android apps, with strategy games being an area of particular interest.
This entry was posted in Java and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s