To blog |

Who is calling this method?

July 3, 2013 by Ivo Mägi Filed under: Java

Have you ever stood in front of a piece of code and wondered why it behaves the way it does. And wished you had the information about the possible locations from which this method was invocated. Back in the old times it might have been easy – static code analysis could have given you the answer. But nowadays when we have reflection and bytecode modification tooling at hand the answer is often not so simple.

Well, we have lived with the same problem for a long time. In Plumbr we are making your lives easier by surfacing the sources of the problems. As a sample – when you happen to have a memory leak in your application, we tell you where the leaking objects are created. Down to the line in source code where the object was created. But we have not been able to hand over the whole call stack so far – it is just too expensive to gather this information during the application runtime.

But there is a very nice tool available just for the task at hand called BTrace. This is a dynamic tracing tool which you can attach to your Java process during runtime to acquire a call stack and then disconnect when you have acquired the information you needed. Btrace dynamically instruments the class bytecode in the target application in order to inject tracing code. You can use the same tool for other amazing purposes, but for the sake of clarity, lets just stick with this feature.

Lets go through a case study using the very same leak report acquired from Plumbr demo application. From the leak report below, we can see that something fishy is happening at the line 27 in the LeakingInterceptor class preHandle() method.

Call stack screenshot

When exploring the source code, we can see that a new ImageEntry instance is created in the line 27:

lastUsedImages.add(new ImageEntry(image));

To get the full stack trace of this situation, we have to give a hint to BTrace about what to monitor in our source code. We do this in a form a small Java class, which will print out the entire stack trace every time a new instance of ImageEntry is created. Code for this is rather straightforward as you see:

import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;
class LeakStack {
   @OnMethod(clazz="org.springframework.samples.petclinic.web.ImageEntry", method="<init>")
   public void printStack() {

The printStack() function is executed every time when a new ImageEntry instance is created. The mapping is in the annotations which specify the class and the method to monitor. And as you might recall from your bytecode classes, “<init>” denotes the constructor.

Now, hopefully you have managed to download the BTrace and it’s time to start the demo application (run “” to start the application and “” script to generate usage). After that we can find out the PID of our demo application with the help of “jps” command.

In order to run btrace executable (found in $BTRACE_ROOT/bin/ folder), we have to specify the PID of our application and our desired script.

$ ./btrace <PID_OF_DEMO_APP>

After that, we should get stack traces printed out to our console similar to the following:


And we have successfully acquired the full call stack from the running application. Those of you who are now confused and worried that running Plumbr is complex – dont stress yourself. Out of the thousands of leaks we have discovered, less than ten were complex enough to hunt down without the full call stack. Plus – our RnD is working to improve our solution to the extent where you do not need to go through such tricky exercises. They just need a bit more time. And maybe a little bit of inspiration as well.

If you managed to read past this last call stack, you are definitely going to enjoy our future posts. So subsribe to our RSS feed or follow us in Twitter and stay tuned.



Your posts are always very interesting. Thanks.

In your second last paragraph, you say “Out of the thousands of leaks we have discovered, less than ten were complex enough to hunt down without the full call stack.”. I think you may want to revisit the wording. As is, it is not much of an endorsement for Plumbr. 🙂


Thanks for pointing it out. Rephrased our intentions in a (hopefully) more meaningful way.

Ivo Mägi