eG Monitoring
 

Measures reported by JvmMemoryLeakTest

Java implements automatic garbage collection (GC); once you stop using an object, you can depend on the garbage collector to collect it. To stop using an object, you need to eliminate all references to it. However, when a program never stops using an object by keeping a permanent reference to it, memory leaks occur. For example, let's consider the piece of code below:

In the example above, we continue adding new elements to the list memoryLeakArea without ever removing them. In addition, we keep references to the memoryLeakArea, thereby preventing GC from collecting the list itself. So although there is GC available, it cannot help because we are still using memory. The more time passes the more memory we use, which in effect requires an infinite amount memory for this program to continue running. When no more memory is remaining, an OutOfMemoryError alert will be thrown and generate an exception like this:

Exception in thread “main” java.lang.OutOfMemoryError: Java heap space at MemoryLeakDemo.main(MemoryLeakDemo.java:14)

Typically, such alerts signal a potential memory leak!

A memory leak can diminish the performance of your mission-critical Java applications by reducing the amount of available memory. Eventually, in the worst case, it may cause the application to crash due to thrashing. To avert such unwarranted application failures, it is imperative that memory leaks are detected at the earliest and the objects responsible for them accurately isolated.

This test continuously monitors the JVM heap usage and promptly alerts administrators when memory usage crosses a configured limit. The detailed diagnostics of the test will then lead you to the classes that are consuming memory excessively, thereby pointing you to those classes that may have caused the leak.

Note:

This test will work only if the following pre-requisites are fulfilled:

  • The test should be executed in an agent-based manner only.

  • The target Java application should use the JDK/JRE offered by one of the following vendors only: Oracle, Sun, OpenJDK. IBM JDK/JRE is not supported.

  • The monitored Java application should use JDK/JRE 1.6 or higher.

Warning:

This test is CPU & Memory intensive and can cause slowness to the underlying application. It is hence NOT advisable to enable this test on production environments. It is ideally suited for Development and Staging environments.

The measures made by this test are as follows:

Measurement Description Measurement Unit Interpretation
Total_allocated_bytes Indicates the total amount of memory space occupied by the objects that are currently loaded on to the JVM. MB  
Leak_suspects Indicates the number of classes that are memory leak suspects. Number Use the detailed diagnosis of this measure to know which classes are using more memory than the configured PCT HEAP LIMIT.

Remember that all applications/classes that throw OutofMemory exceptions need not be guilty of leaking memory. Such an exception can occur even if a class requires more memory for normal functioning. To distinguish between a memory leak and an application that simply needs more memory, we need to look at the “peak load” concept. When program has just started no users have yet used it, and as a result it typically needs much less memory then when thousands of users are interacting with it. Thus, measuring memory usage immediately after a program starts is not the best way to gauge how much memory it needs! To measure how much memory an application needs, memory size measurements should be taken at the time of peak load - when it is most heavily used. Therefore, it is good practice to check the memory usage of the ‘suspected classes’ at the time of peak load to determine whether they are indeed leaking memory or not.

Total_live_instances Indicates the number of objects present in the JVM. Number Use the detailed diagnosis of this measure to view the top-20 classes in the JVM in terms of memory usage.
Total_classes Indicates the number of classes currently present in the JVM. Number  
Classloders Indicates the total number of components of this category that have been deleted. Number  
Gc_roots Indicate the number of GC roots currently present in the JVM. Number A garbage collection root is an object that is accessible from outside the heap. The following reasons make an object a GC root:
Reason Description
System Class Class loaded by bootstrap/system class loader. For example, everything from the rt.jar like java.util.
JNI Local Local variable in native code, such as user defined JNI code or JVM internal code
JNI Global Global variable in native code, such as user defined JNI code or JVM internal code
Thread Block Object referred to from a currently active thread block
Thread A started, but not stopped, thread
Busy Monitor Everything that has called wait() or notify() or that is synchronized. For example, by calling synchronized(Object) or by entering a synchronized method. Static method means class, non-static method means object
Java Local Local variable. For example, input parameters or locally created objects of methods that are still in the stack of a thread.
Native Stack In or out parameters in native code, such as user defined JNI code or JVM internal code. or reflection.
Finalizer An object which is in a queue awaiting its finalizer to be run.
Unfinalized An object which has a finalize method, but has not been finalized and is not yet on the finalizer queue.
Unreachable An object which is unreachable from any other root, but has been marked as a root by MAT to retain objects which otherwise would not be included in the analysis.
Unknown An object of unknown root type.
Finalization_objects Indicates the number of objects that are pending for finalization. Number Sometimes an object will need to perform some action when it is destroyed. For example, if an object is holding some non-java resource such as a file handle or window character font, then you might want to make sure these resources are freed before an object is destroyed. To handle such situations, Java provides a mechanism called finalization. By using finalization, you can define specific actions that will occur when an object is just about to be reclaimed by the garbage collector.

A high value for this measure indicates the existence of many objects that are still occupying the JVM memory space and are unable to be reclaimed by GC. A consistent rise in this value is also a sign of a memory leak.