Memory Leak Testing- Why it is important? How is it done?

What is Memory Leak?

In simple language a memory leak is loss of available memory when a program fails to return memory that it has obtained for temporary use. As a result, the available memory for that application drains out completely and the programs can no longer function. For a program that is frequently opened or that runs continuously, even a very small memory leak can eventually cause the program to terminate. A memory leak is the result of a programming bug, so it is very important to test it during development phase.

Also, a constantly increasing memory usage is not necessarily evidence of a memory leak. Applications may store some amounts of information in memory in form of cache. If the cache grows increasingly to cause problems, this may be a programming or design error, but is not a memory leak as the information is not used much.

Garbage Collection (GC) Roots are the objects that are always reachable. If an object is always reachable, then it is not eligible for garbage collection. GC Roots therefore are always ineligible for collection. Other objects on the heap reachable from the GC roots are considered to be live objects, and ineligible for garbage collection, the objects that are unreachable can be marked for reclamation.

Ok, now what is Heap? A heap is a form of data structure that is similar to a tree. It is basically an area of memory used for dynamic memory allocation. When an application needs to use a certain amount of memory temporarily it can allocate the memory from the heap and when this is no longer needed it can be freed up. Once the memory is freed it can be reused for future allocation.

Garbage collector collects those objects that are not GC roots and are not accessible by references from GC roots.

In a Java based application, Garbage collector reclaims objects that are not used. It’s upto Java Virtual Machine (JVM) to decide when to automatically run the Garbage collector. During the usage of an application JVM periodically verifies which objects in memory are still being used and which are not. Unused objects are discarded and memory is reclaimed and reused again. This process is called garbage collection.

If there is no memory space present for creating new objects in Heap. Java virtual machine throws OutOfMemoryError or Java.lang.OutOfMemoryError. Basically, it is nothing but a bug in program that prevents it from freeing up memory that is no longer needed. As a result the program utilizes more and more memory until it finally crashes because there is no more memory left.

As a smart QA, you will not want these issues to happen in your application. So before a customer reports it, QA should have the insight to catch the error beforehand. So, here comes in the picture of Memory Profiling. In computer programming memory profiling is a form of dynamic program analysis that measures the memory usage of the application.

There are various tools available to do memory profiling. Ex: To profile java applications some of them are:

Among many other features available in tools for memory profiling, a common feature of all these tools is “Forced garbage collection”. Let say you are testing for memory leaks in a Java based application, so without waiting for JVM to automatically run the Garbage collector, we can explicitly force garbage collect using memory profiler tools to learn whether memory usage returns to the level at which it was before.

Memory telemetry is one such graphical feature available in Yourkit Java Profiler tool. After you are connected to the profiled application, the “Memory” telemetry tab on the session panel shows live memory usage statistics like:

  • Heap memory usage (heap memory is the storage for Java objects)
  • Non heap memory usage (non-heap memory is used by Java to store loaded classes and other meta-data)
  • Current class loading statisticsWe can also take a Memory snapshot using Yourkit Java profiler. A memory snapshot represents the memory state of the profiled application at the moment it was captured. It contains information about all loaded classes, about all existing objects, and about references between objects.

This image below shows where to capture Memory snapshot in Yourkit.



Analyzing Memory Snapshot:
There are different views such as CPU, Threads, Memory, Garbage Collection etc. that helps us analyze the data in the snapshot.

Analyzing Memory Snapshot

Analyzing Memory Snapshot

If you want to find the leak or, more importantly, fix it, you really need to let it grow. Small memory leaks are lost within all those objects that are present in an application at any given point of time. Regardless of the tool you use for identifying memory leak, there should be lots of objects that are no more useful.

This means that your application should run for a significant period of time and as many different parts of your application should be executed as possible. Otherwise there will be no significance in finding memory leaks. One such way is to run Memory profiler and carry out testing for memory leak during performance testing the application covering the entire work flow.


Written By: Bijit Pattanaik , QA Engineer, Mindfire Solutions


Posted on March 28, 2014, in Manual Testing, Security Testing and tagged , , , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: