Profiling Java Code

short URL

From Fiji

Jump to: navigation, search

If your Java code seems to have bottlenecks that make it run slow, you need to profile the code. There are a couple of options you have for that.

Contents

JProfiler

JProfiler is a commercial program, but you can evaluate a trial version.

HPROF

Use HPROF, a profiler included in Sun's Java since version 1.5.

VisualVM

Use VisualVM (a tutorial can be found here), which is a graphical alternative to HPROF, available in Sun's Java since version 6 update 7.

See also Rejeev Divakaran's detailed instructions on memory profiling with VisualVM.

OProfile

If you are on Linux, you can use OProfile, a low-overhead profiler which uses a kernel module to minimize its impact.

You need to recompile OProfile for Java support (at least on Ubuntu, there is no precompiled OProfile package with Java support). On Linux, you can use this shell script to compile and install it:

sudo apt-get install libpopt-dev binutils-dev checkinstall && 
cd $FIJI_ROOT && 
git clone contrib@fiji.sc:/srv/git/oprofile && 
cd oprofile/oprofile && 
./autogen.sh && 
./configure --with-java=$(pwd)/../../java/linux-amd64/jdk1.6.0_22/ &&
make && 
sudo checkinstall

You should make sure that the path to the JDK is correct. (But you do not need to use checkinstall, but it will make it easy to remove the package once you do not want it anymore.)

Start the profiler

sudo opcontrol --start --no-vmlinux

Recent versions of OProfile require that a user account oprofile with default group oprofile exist before starting the OProfile daemon. On current Ubuntu systems, you can make one by calling sudo adduser oprofile (Ubuntu creates a default group of the same name for each new user account). If you do not have such an account, you will get something like anon (tgid:10014 range:0x100000-0x103000) in the report.

Start Fiji with support for profiling

fiji -agentpath:/usr/local/lib/oprofile/libjvmti_oprofile.so --

Stop the profiler

sudo opcontrol --stop

Getting at the profiling data

sudo opcontrol --dump && 
opreport -l image:\*/fiji

If you get entries like "anon (tgid:10014 range:0x100000-0x103000) you probably did not create a user account oprofile in a group oprofile before starting the OProfile daemon.

To get information about source files and line numbers, also pass the -g option to opreport.

Further reading

See the OProfile manual for more information.

Shark (for MacOSX)

You might find Shark 4 useful if you're on MacOSX.