White Box Testing
Test your code
   Home      Memory leaks in Java
 
Memory Leaks in Java
 
Java Virtual Machine (JVM) and its garbage collector (GC) manages most memory , it is possible to have memory leaks in complex java code. Although memory management might be said to be automatic, it does not free the programmer from thinking about memory management issues.
  • Bugs in third party libraries

 

  • Bugs in JDK garbage collection implementation

 

  • Circular Backward references

 

  • Saving an object reference in a class level collection and forgetting to remove it at the proper time.

  • Resource/Handle leaks
Resource/Handle leak occurs If handle acquired for resources such as file,IO not closed For example following code
Scanner input = new Scanner(System.in);
handle leak occurs if we forget to close Scanner handle input.close();
 
  • Static object references and classes
Static object references and classes always reside in memory for the lifetime of application. Final static fields are more problematic as once they are initialized , they can not be changed or nullified. Uses of static object references and classes should be minimized
 
  • Thread-local variables leaks
These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable. Thread-local instances are typically private static fields in classes that wish to associate state with a thread (e.g., a user ID or Transaction ID).
Thread-local uses a WeakReference internally. If the Thread-local is not strongly referenced, it will be garbage-collected.If you have a Thread-local as a final class member, or some sort of backward references that's a strong reference, and it cannot be collected until the class is unloaded. In most of application threads are re-used via thread pool and thus never garbage collected, that’s why we need to be careful to clean thread local variable
 
Thread-local variables are stored in a special map called Thread-localMap which is designed to hold thread local objects. And thus Thread-local variables leaks can be traced easily by looking  Thread-localMap in Heap dumps 
 
  • Failure to Clean-Up or Free Native System Resources
The automatic garbage collector does a fairly good job of cleaning up unused object instances. There are other ways to allocate memory from the Java VM that exist outside the bounds of Java instances—typically, in the form of native system resources. Native system resources are resources that are allocated by a function external to Java. These allocations are typically done through the Java Native Interface (JNI), and are implemented in C or C++. the native methods may need some explicit cleaning-up calls from Java. If these calls are not made, more memory leaks can occur .
 
Tracing Memory Leaks in java
 
Now that you know about different ways that memory leaks occur in Java programs, you need to know how to detect them.
 
The first way is to use an operating system process monitor, which tells how much memory a process is using. See if your Java process grows in memory over time.
 
The common method is to use heap dump analysis to see heap uses and to rectify heap issues
 
You can use the methods in the Java Runtime classes to debug your code, which tell you how much total heap memory is being manipulated.
 
Finally, you can detect memory leaks using memory-profiling tools such as JProbe, or JInsight. These tools allow you to examine your running program, providing valuable information, such as the number of instances allocated for each type of class.
 
Java heap analyzer helps you find memory leaks and reduce memory consumption.Use the Memory Analyzer to analyze productive heap dumps with hundreds of millions of objects, quickly calculate the retained sizes of objects, see who is preventing the Garbage Collector from collecting objects, run a report to automatically extract leak suspects.
 
VisualVM is a tool that provides a visual interface for viewing detailed information about Java applications while they are running on aJava Virtual Machine (JVM). VisualVM organizes JVM data that is retrieved by the Java Development Kit (JDK) tools and presents the information in a way that allows data on multiple Java applications to be quickly viewed—both local applications and applications that are running on remote hosts. Programmers can also capture data about the JVM software and save the data to the local system, and then view the data later or share it with others. VisualVM is built on the NetBeans Platform; its architecture is modular and easy to extend with plugins.