An easy way to discover memory leaks in the apps you deploy on Jelastic
Have you ever had to wrestle with memory leaks? Do you get shivers down your spine when you recall the last time you had to work a 100-hour work week, just to understand what brings your application to its knees? If yes, then this article is for you. We have worked together with the Plumbr team to verify their memory leak detection tool works well for Jelastic deployments.
Plumbr is different than all the profilers, heap dump analyzers, and APMs you may have used in the past. It analyzes your application behavior during runtime and, when it detects a leak, it provides the size of the leak, where leaking objects are created, and where they are being referenced; down to the line number in your source code! As a picture is worth a thousands words, here is an example of what a leak report might look like:
From this report, we can see that Plumbr has discovered a leak which occupies 39MB of the tiny 161MB available for this application. There are already 744 instances of ImageCache, which have leaked, and those instances were all created in line 26 in method preHandle() in class LeakingInterceptor. And all those leaked ImageCache objects are referenced from a HashMap, which is an instance variable in a LeakingInterceptor class.
Equipped with data like this, it doesn’t take endless days to find and fix leaks, but mere minutes instead. Or so the Plumbr guys promised. We wanted to find out for ourselves whether this is really so easy.
To start, we registered at http://www.plumbr.eu. Within minutes, we had our hands on the Plumbr trial version. It ships without strings attached and is 100% free to try. If Plumbr doesn’t find any problems, there is a strong chance you don’t have a leak. As of the writing of this article, the Plumbr guys state that they find 88% of existing leaks. When Plumbr finds something suspicious and you wish to see the details, similar to the sample report above, this is when you pay.
The Plumbr download unzips with the following structure:
- demo/ – small application to test out Plumbr features
- EULA.txt – end user license agreement
- lib/ – native libraries for different supported operating systems (Linux, Windows, Mac, Solaris)
- plumbr.jar – Plumbr itself, packaged as a -javaagent
- plumbr.lic – Plumbr license
- README.txt – A dangerous virus
I created an environment in Jelastic using the Tomcat application server. Next, I had to find an application to deploy and check. In this case, I used the WAR file that ships with the Plumbr distribution package to demonstrate leak discovery. Following the few easy steps described in the Jelastic tutorials, I deployed my application easily.
Next, I used the instructions in the Plumbr manual to attach Plumbr to our Jelastic environment. The following steps describe the three simple operations I performed:
1. Open the application server configuration and upload the plumbr.jar file to the /home/ folder.
2. Plumbr also needs binaries to gather data from the JVM, so you must add the right binaries. As Jelastic is running on 64-bit linux, the file you need to upload is the lib/linux/64/libplumbr.so file within the Plumbr distribution. Upload it to the same /home/ folder.
3. After you have successfully uploaded both files, find the /home/server/variables.conf file and specify the location of the javaagent and the agentpath variables to -javaagent:/opt/tomcat/temp/plumbr.jar and -agentpath:/opt/tomcat/temp/libplumbr.so respectively. The configuration is visible in the next screenshot. Note that the paths specified do not point to the sandboxed /home/ but to the /opt/tomcat/temp/ instead.
Now, we’ll check whether Plumbr was successfully attached. The easiest way to do this is to check your Tomcat logs. Navigate to the /catalina/catalina.out file, and you should see a banner similar to the following:
* Plumbr (2.0) is attached. *
* Running with JRE from /usr/java/jdk1.7/jre *
OK! Let’s see whether the application contains some leaks!
Plumbr works by tracking the creation and destruction of every object and then applies machine learning-guided algorithms to find out what is normal for your application and what looks suspicious. For Plumbr to find the suspects, we need to provide enough data. In my case, I simply launched the load tests packaged along with the Plumbr demo application. After about five minutes of runtime, Plumbr notified me (via the email I used to register), that memory leaks had been found. The leak report was attached to the email, but can also be found in your Jelastic environment, in the reports folder as seen in the screenshot below.
And, that’s it! We’re done. We’ve created a brand new application server instance, deployed an application, and attached a Plumbr as a javaagent. Plus, we found a leak in the application. All this, in 30 minutes or less. With maybe a little Googling involved. Not bad.
So, in conclusion: we were able to download and attach Plumbr to our Jelastic environment in under 30 minutes. We were able to find our first leaks (in their demo, but nevertheless) in less than 15 minutes. When you compare this to the endless hours spent trying to find your needle in the gigantic Java EE haystack, we’d say this isn’t bad at all. So, next time you are facing a difficult memory leak, consider giving Plumbr a try!