The Java profiling world can be a somewhat arcane maze of GUI’s, most of which seem to make things more complex.
Fortunately, it’s actually quite simple to get a usable, sample based CPU profile from any modern JVM. Simply run your program with the additional flags:
Now, when your program exits, the JVM will also emit a java.hprof.txt file with a listing of where time was spent. If you pore over that file, you’ll eventually find out where your program was wasting it’s time.
But it turns out there is much simpler option – gprof2dot.py. This lovely little utility can convert your grungy hprof output to a beautiful dot graph. (N.B. I wrote the hprof format importer for gprof2dot, so blame me if it’s wrong.)
gprof2dot.py < java.hprof.txt | dot -Tpng > profile.png
Gives us back:
In this case, it appears that:
- Using Java regular expressions to split things is slow
- I need to speed up my cosine similarity calculation
xdot.py is another useful program for interactively viewing these graphs: simply feed the output of gprof2dot (or any other graphviz generator) to it:
gprof2dot.py < java.hprof.txt | xdot.py
And now you can scan around your profile image directly.