How can I tell what is using the memory when there is a heap overflow in Java?
I know a little about profiling, but what I am partic开发者_如何学Goularlly insterested in, is what has all the memory when I get these heap over flow exceptions.
I will start getting them after about a hour of debugging. I am hoping there is some sort of dump or something, that I can use to get a list of what instances are around at the time the program starts.
By the way, sorry if this is a lazy question. I really shoud put sometime in learning about profiling.
Grae
You can use jmap -dump
to generate heap dumps. And you can analyse the heap dumps using a variety of programs, such as jhat or VisualVM (both of which come with the JDK).
There, lots more things for you to Google up now. :-)
Note that the type of the OutOfMemoryError is important. There are many variations of it. Read the message that comes with the exception. It may be "Java heap space", which indicates a heap related error, but it can also have another message, referring to other memory areas (See this post for example).
Assuming that there's a heap alocation problem, you can use the JVM flag XX:+HeapDumpOnOutOfMemoryError in order to get a full heap dump when the error occurs. Then, you can use some heap dump reader and analyze the results (VisualVM or jhat for example).
As the other response mentions, you can use the command line tool jmap as well. I would start with jmap -histo, which creates a simple histogram of the different classes. If you see a class with an unreasonable number of instances, you have a memory leak suspect.
Install the JRockit VM and then use JRockit command center :) It's really, really good, and gives a list of method names and the CPU time these methods are consuming, as well what objects are floating around in the system.
One of the most useful tools in the Sun Java 6 JDK is "jvisualvm" which can attach to a previously started process and do the memory profiling you need to do to resolve this issue.
(You can also download VisualVM manually, but the JDK is probably the easiest way to get up and running).
See http://download.oracle.com/javase/6/docs/technotes/guides/visualvm/applications_local.html for initial usage notes.
VisualVM is free and useful. JProfiler and YourKit are non-free and more useful.
精彩评论