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.
In past, I encountered the following error -
ReplyDeletejava.lang.OutOfMemoryError: PermGen space
This is something rare, I feel. Most of the time, the OutOfMemoryError comes with heap space.
The application was running as a server class jvm with jdk 1.6. By default, the permanent generation space is 64 Mb and it is sufficient for most of the real-time applications. But, if your code base is large and the VM is loading many classes or your application is using too many third party libs (these libs might be large in size and make the VM to load many more classes) - the default size might not work for your application.
You can try increasing the permgen space and visualize it through some profiler or full GC logs. I prefer visualvm that comes with jdk. You can keep increasing the permgen space (by 128 Mb at a time) till 50% space is free at the peak usage of the application. - The 50% free space is just a comfort cushion.
The max permgen space can be increased by command line option. For example -
-XX:MaxPermSize=256m