How to find Memory Leak in java web application

Posted by Arun
My name is Arunkumar, I was born and raised in Trichy, Completed B.E Computer science at Jayaram College of En...
User is currently offline
on Saturday, 30 June 2012
in Performance Tuning

How to find Memory Leak in java web application:

Java implements automatic garbage collection (GC), and once you stop using an object you can depend on the garbage collector to collect it.

How do memory leaks happen in Java?
Memory leaks occur when a program never stops using an object, thus keeping a permanent reference to it.

There are various tools like Jprofiler,JConsole,VisualGC,VisualVM,JMap,JHat are available to find memory leak. I have almost used all these tools and personally suggest JProfiler tool. This tool provides more detailed information to debug the memory leak.

I will share one of my experiences how I fixed memory leak in our application using jProfiler.

We deployed our application to one of our big clients and it was running smoothly. We support REST api for backdoor operation and our client wrote a python script which invoked n number of concurrent threads and performed various operations continuously without any sleep time between the threads.

When they run the script, the memory gets started increasing gradually and at over period of time it reaches the limit and thows java.lang.OutOfMemoryError: Java heap space at outofmemory.Large. Since its production environment it became a very high priority issue and we need to be fix this asap.

Finding a memory leak in an application is not that easy.

We need to know what exact operation they are performing and need to set up the same scenario in our dev environment for debugging. We need to setup the same load in database since this might even cause the problem because of fetching huge data from Database.

Steps followed to find the memory leak:

Created the same client's production environment in my development machine.
Ran the same python script and reproduced the Java heap space OutOfMemoryError in my machine.
Hooked JProfiler and ran the same python script and recorded the objects in JProfiler.
From JProfiler using Heap Walker i walkthough the biggest objects and after a period time I manually ran the GC and found the long living objects in jvm. See the below image

Then i found from where those objects are created and why it was living for long time in my application code using JProfiler.

Finally I found the complete memory leak scenario and fixed the same.

The scenario is the python script which perform authentication and CURD operations. When performing authentication a session object is created. Since Python script doesn't accept cookies it started creating new session object for every authentication and maintained session scope for about 10hrs(becoz of session timeout in web.xml).So GC couldn’t clear those session objects. Script performed millions of authentication operation which resulted in creating millions of session objects and hit the maximum memory and finally throws java heap, java.lang.OutOfMemoryError.


Follow us on Twitter

Most Popular Post

Thread level CPU monitoring for Java process using JTop Plugin in JConsole : There may be some situations where our java application might hang or CP
JVM is the heart of any Java based Application Server. Java memory is now divided into 3 generations: 1.Young      Eden      Two survivor spac
After deploying our java web application in tomcat server we need to monitor the current execution of our application.This can be easily done by viewi
What is JIP - Java Interactive Profiler ? We do write a lot of coding in our development but we are not sure that our coding may lead to performance
How to find Memory Leak in java web application: Java implements automatic garbage collection (GC), and once you stop using an object you can depend

Top Blog Entries

No blog entries yet