A lot of programmers think that memory management is an old task from the C programming times. They believe that no memory control is required after Garbage Collector arrived to ours lives. That is almost true; Java is a good language that takes cares of this uncomfortable detail. But… always? NO.
It is very common to watch big installations at big customers that need to be rebooted every night, only to avoid this problem. But this is only a grandma solution. Memory leaks are usually known after java.lang.OutOfMemoryError, but this is not usually the problem: the main problem is the performance impact that this delivers to the affected platform during long time before the OutOfMemoryError is got.
A typical solution is the postmortem analysis. Although it sounds as a reasonable approach, it has a problem; analysis is some one who dead before: you need to wait until your system crashes in production time, grab a big file of 3 or 4 GB and put this information in an even bigger sized server, and take your time … no cafeine drinks are recommended.
Lucierna distributes a nice and smooth tiny solution: The Antorcha Memory Plumber. It is such an elegant solution because Memory Plumber only looks where the Java Memory Leaks usually are. Antorcha Memory Plumber monitors Collections and Maps, looking which one is growing up with no sense, and its full stack path, and reports this to you.
The installation process takes only two steps:
-Add “-javaagent:aspectjweaver.jar -cp AntorchaMemoryPlumber.jar” parameter in your jvm or app server startup script.
-Run your app under load conditions for 30 minutes and read the results from the stdout.
StandAlone Demo App
The usage of Antorcha Memory Plumber for a standalone Java application is easy: just download the zip file, uncompress it; there you will find everything required to run it properly, including third party libraries as well as licenses and some examples.
In order to provide you a quick start kit, a leaking class, called “abr” is included as well. In the next snapshot you can review the standard output that you get when running the “abr” leaking class:
WebApp running under App Server
Installation for any Java AppServer is easy, and basically requires the same: add the -javaagent with the AspectJ weaver, and put the Antorcha Memory Plumber in the class path, you can either use the ‘-cp’ option, or add to the CLASSPATH env variable. Depending on your application server the steps can be slightly different.
For Tomcat APP server here you are the details:
1) Copy the aspectjweaver.jar and antorchaMemoryPlumber.jar to any folder Tomcat user can access, for instance /opt/Tomcat/antorcha
2) Modify the startup script /opt/Tomcat/bin/startup.sh, including:
PRGDIR=`dirname "$PRG"` export JAVA_OPTS="-javaagent:/opt/Tomcat/antorcha/aspectjweaver.jar"
Then be sure to add to the class path the file /opt/Tomcat/antorcha/antorchaMemoryPlumber.jar, you can do it modifying the setclasspath.sh script or just copy it to TOMCAT_HOME/lib folder (as a quick approach).
How does Antorcha Memory Plumber work?
Antorcha Memory Plumber works using Java byte code instrumentation, based on AspectJ, to modify all the methods calling to collections and mapping operations. Tracking any addition, and then sampling those collections or maps that are constantly increasing. After some stages it verifies the object leaks, and provides you not only the leaking connection or the last method where the collection/map was increased, but also the full stack trace, so you can even easily identify leaks in Third Party Libraries, HttpSessionLeaks and so on.
As you can imagine, in order to allow Antorcha Memory Plumber to work, you will need to stress test your tool during the time that it is working (aprox. 27 minutes). The Full version of Antorcha Memory Plumber includes auto tune capabilities to setup proper heuristic thresholds like hunting time, etc; but with this “Light” version you should be able to find any leaking class always when you implement a Collection/Map interface (the most usual cause of memory leaks) and you achieve at least a total amount of 500 elements in that collection/map while Antorcha Memory Plumber is up.
* amp.period.startup.delay: Minutes that the memory plumber will wait prior starting to search for leaks. If not set the default value is 2.
* amp.collectionelements.minimum: Minumum number of elements for that collection that will be needed to start to be considered as a leak. If not set the default value is 500
* amp.period.suspect: Time in minutes on where a collection will be treated as suspect, after that time, the collection is removed from the suspects if that collection decreases. If not set the default value is 10
* amp.iterations.numberof: Number of iterations on that the memory plumber will seek for any potential leak. If not set the default value is 3
* amp.iterations.period: Wait time in seconds per iteration. If not set the default value is 5
Setting a delay of 4 minutes before starting to look for leaks and a suspect period of 20 minutes, leaving the rest options to their default values.
java ….. -Damp.period.startup.delay=4 -Damp.period.suspect=20 …..
Download is easy: if you are not a registered user you will be asked to register which is a very straightforward process: just include your e-mail, name, surname, country and company: that’s it. Once you are registered you have to log-in and then you will be able to download Antorcha Memory Plumber.
In case you find it useful, interesting, if you have doubts or whatever, please post a comment, your questions and feedback are always welcomed.