Do-It-Yourself Java Profiler

This evening I got a bug: I wanted to see how easy it would be to implement a profiler using the new Java 1.5 java.lang.instrument API. Not being a native speaker of JVM bytecodes, I also downloaded the Javassist package from the JBoss folks, and started tinkering.

A few hours later, I was able to run various Java programs, and get a dump of object allocations and deallocations as they ran. All with a couple of Java classes that get to transform the bytecodes of classes as they are loaded. Sweet! Most of the hard work centered around figuring out Javassist, which at times produced the telltale impulses of wanting to rip my hair out. But once I worked through those issues, it was remarkably smooth sailing!

This was mainly a proof-of-concept, but now that I know it works, I’ll have to refine this thing! The return on my efforts is pretty high, because of how easy this has all become. It amazes me, what you can do with these things by now.

Sigh, now to get only five hours of sleep, again…

2 Responses to “Do-It-Yourself Java Profiler”

  1. Sebastian Says:

    I donnie, First of all i want to tell you i was looking for something and the first interesting explanation I ound was yours! Thank you for writing such a interesting thing, without charging to read!!

    I am interested in creating my own java profiler, for java 1.6, but i have no idea how to start.

    Could you please give me some hints?

  2. guga Says:

    Hi,

    Nice article, that really explains the subject, and doesn’t confuse even more like som many articles on the web.

    Thank you.

    Now for that javassist article … :)

Leave a Reply