In recent times, I have encountered java.lang.OutOfMemoryError
exception while running an application.
During one such instance, I was able to get the heap dump using jvisualvm
.
I am able to open the .hprof
heap dump file obtained from the heap dump using NetBeans 8.1
IDE but I am not aware of how to analyze the data dump. I'd like to know how to read the dump file and take corrective actions to reduce the out of memory exception from an application perspective.
In general, basically what you do is analyze "what is using the most RAM"? then when you've figured that out (and "is it probably the problem of me running out of RAM?") then you try and figure out why there are so many of those objects around. Are they being referenced by something that is holding onto objects but needn't? Or that is accidentally holding onto references of thigns it shouldn't? Are you using too large of archicture/paradigm (ex: storing "everything in one big array")? Is your database client "buffering" large ResultSets into RAM before returning them? Etc...
The tool you need for this case is this app:
Memory Analyzer Tool
Just download & start, then load your hprof file. It may take a minute or two depending on the size of your hprof, but then you will be presented with a nice analysis on your memory usage. It is very easy to use, and automatically highlights the potential memory leaks, performs analysis on the data from different angles.
I am using MAT exclusively when I am dealing with non-trivial memory issues, and I solved all of these issues as far as I remember.
There are many ways to find the root cause of a memory leak, like using a profiler such as JProfiler and simply applying what is described in this great video. You could also have a look to Eclipse Memory Analyzer also know as MAT that will be able to analyze your heap dump and propose potential causes of your memory leak as you can see in this video (you can find more information about the Suspect Report here). Another way could be to use Java Flight Recorder by applying this approach. Or using JVisualVM using the approach described in this video.