Over a million developers have joined DZone.

Who is calling this method?

Download Forrester’s “Vendor Landscape, Application Performance Management” report that examines the evolving role of APM as a key driver of customer satisfaction and business success, brought to you in partnership with BMC.

Originally Authored by Kristjan Reinloo.

source code bug

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:
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.

Originally authored by
Now, hopefully you have managed to download the BTrace and it’s time to start the demo application (run “start.sh” to start the application and “create_usage.sh” 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 LeakStack.java
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.

See Forrester’s Report, “Vendor Landscape, Application Performance Management” to identify the right vendor to help IT deliver better service at a lower cost, brought to you in partnership with BMC.


Published at DZone with permission of Nikita Salnikov-tarnovski, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}