How to analyze heap data from .hprof file and use it to reduce memory leaks?

南楼画角 提交于 2019-12-05 07:08:13

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.

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.

Most of the time all you need to know is which are the classes most at fault of chewing up memory. You can use: jmap -histo against a running process, which is handy if it's a large JVM...you don't want to be fiddling around with large heap-dump files. It must be run as the same Linux user that owns the process, so e.g. in Linux you can use:

sudo -u <user> jmap -histo <pid>

Obviously "histo" is short for "histogram". This will dump a histogram to stdout, so you probably want to pipe it to a file for analysis. It'll be sorted by the number of instances * size of instance, so look at the top 10 entries and you might have your answer.

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...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!