I recently encountered java.lang.OutOfMemoryError in an application at production level. This makes the application totally unavailable to the users. If the application is too critical, usually, we tend to restart the application and make it available immediately to the users and think of debugging it through previous logs.
In order to debug the issue in the right direction, we should try to complete the following before restarting the application -
1. Get the heap histogram through jmap (a java command line tool) as -
<JAVA_HOME>/bin/jmap -histo:live <pid - application process id> > <filepath where you want to save the snapshot>
2. Take the thread dump.
on unix -
> kill -3 <application pid>
The above would produce the thread dump to the console (stdout) attached to the application. It would be there in your log file only if you are redirecting stdout to your log file.
3. Get the heap dump -
If you have HeapDumpOnOutOfMemoryError VM option enabled, then its great and it will produce the dump in a file. You can also enable this option while your application is running -
<JAVA_HOME>/bin/jinfo -flag +HeapDumpOnOutMemoryError <pid>
Heap dump can also be obtained as -
<JAVA_HOME>/bin/jmap -dump:live,file=heap.out,format=b <pid>
4. Run top command (on unix) and save the snapshot.
The above are the minimal steps that we should perform before restarting the application. This data collection would guide us in the right direction while debugging the issue.
The heap histogram would tell us what the top objects are which acquire maximum heap (usually we are interested in objects that belong to application classes). We can try to relate the application behavior why these objects are lying there in heap and not getting garbage collected.
The heap dump can be analysed through jhat (though it needs lot of memory to start with). We can see the object graph for the objects occupying most of the heap and can analyse their lifecycle.
The link below might be useful to understand analysing and monitoring the java application -
http://java.sun.com/developer/technicalArticles/J2SE/monitoring/
Let me know about your thoughts and real-time experience with OutOfMemoryError.
In order to debug the issue in the right direction, we should try to complete the following before restarting the application -
1. Get the heap histogram through jmap (a java command line tool) as -
<JAVA_HOME>/bin/jmap -histo:live <pid - application process id> > <filepath where you want to save the snapshot>
2. Take the thread dump.
on unix -
> kill -3 <application pid>
The above would produce the thread dump to the console (stdout) attached to the application. It would be there in your log file only if you are redirecting stdout to your log file.
3. Get the heap dump -
If you have HeapDumpOnOutOfMemoryError VM option enabled, then its great and it will produce the dump in a file. You can also enable this option while your application is running -
<JAVA_HOME>/bin/jinfo -flag +HeapDumpOnOutMemoryError <pid>
Heap dump can also be obtained as -
<JAVA_HOME>/bin/jmap -dump:live,file=heap.out,format=b <pid>
4. Run top command (on unix) and save the snapshot.
The above are the minimal steps that we should perform before restarting the application. This data collection would guide us in the right direction while debugging the issue.
The heap histogram would tell us what the top objects are which acquire maximum heap (usually we are interested in objects that belong to application classes). We can try to relate the application behavior why these objects are lying there in heap and not getting garbage collected.
The heap dump can be analysed through jhat (though it needs lot of memory to start with). We can see the object graph for the objects occupying most of the heap and can analyse their lifecycle.
The link below might be useful to understand analysing and monitoring the java application -
http://java.sun.com/developer/technicalArticles/J2SE/monitoring/
Let me know about your thoughts and real-time experience with OutOfMemoryError.