How to debug Java OutOfMemory exceptions?

It is generally very difficult to debug OutOfMemoryError problems. I'd recommend using a profiling tool. JProfiler works pretty well.

I've used it in the past and it can be very helpful, but I'm sure there are others that are at least as good.

It is generally very difficult to debug OutOfMemoryError problems. I'd recommend using a profiling tool. JProfiler works pretty well.

I've used it in the past and it can be very helpful, but I'm sure there are others that are at least as good. To answer your specific questions: A heap dump is a complete view of the entire heap, i.e. All objects that have been created with new.

If you're running out of memory then this will be rather large. It shows you how many of each type of object you have. A thread dump shows you the stack for each thread, showing you where in the code each thread is at the time of the dump.

Remember that any thread could have caused the JVM to run out of memory but it could be a different thread that actually throws the error. For example, thread 1 allocates a byte array that fills up all available heap space, then thread 2 tries to allocate a 1-byte array and throws an error.

It looks like IBM provides a tool for analyzing those heap dumps: alphaworks.ibm.com/tech/heaproots ; more at www-01.ibm.com/support/docview.wss?uid=s... .

I've had success using a combination of Eclipse Memory Analyzer (MAT) and Java Visual VM to analyze heap dumps. MAT has some reports that you can run that give you a general idea of where to focus your efforts within your code. VisualVM has a better interface (in my opinion) for actually inspecting the contents of the various objects that you are interested in examining.It has a filter where you can have it display all instances of a particular class and see where they are referenced and what they reference themselves.

It has been a while since I've used either tool for this they may have a closer feature set now. At the time using both worked well for me.

These are the tools I use too. I prefer MAT because it needs much less memory to load big heap dumps than VisualVM, even if I have to agree that VisualVM interface is easier to use. – Guillaume Alvarez Dec 22 '10 at 19:06.

You can also use jmap/jhat to attach to a running Java process. These (family of) tools are really useful if you have to debug a live running application. You can also leave jmap running as a cron task logging into a file which you can analyse later (It is something which we have found useful to debug a live memory leak) jmap -histo:live | head -n > Jmap can also be used to generate a heap dump using the -dump option which can be read through the jhat.

See the following link for more details lshift.net/blog/2006/03/08/java-memory-p... Here is another link to bookmark java.sun.com/developer/technicalArticles....

Jstack should print the stack trace of a java process. Download.oracle. Com/javase/1.5.0/docs/tooldocs/share/… – Nauman Dec 22 '10 at 21:12.

Once you get a tool to look at the heap dump, look at any thread that was in the Running state in the thread stack. Its probably one of those that got the error. Sometimes the heap dump will tell you what thread had the error right at the top.

That should point you in the right direction. Then employ standard debugging techniques (logging, debugger, etc) to hone in on the problem. Use the Runtime class to get the current memory usage and log it as the method in or process in question executes.

1 In general, finding the thread that got the OutOfMemoryError is not helpful. Sure, it was the straw that broke the camels neck, but is that last straw always the one to blame? Also, "standard debugging techniques" will help you little if you can not reliably reproduce the problem.

– meriton Dec 22 '10 at 18:36 In my experience (which to toot my own horn is pretty considerable), the OutOfMemoryError generally does occur in the method that is causing the problem. Of course you are correct, its not always the case. But because Java doesn't really suffer from much from memory leaks, these kinds of errors are often due to some heavy processing (text or XML) that is executing in a single Thread and that will be the same one that gets the error.

But you are right, other "innocent" threads could get the error. – Fraggle Dec 23 '10 at 2:47.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions