VisualVM tool provides a direct connection to any Virtual Machine locally or remote, or can load heap dump or thread dump from a file.
To dump heap memory to a file type in console(linux):
- Get java process id
ps -eaf | grep java
- Dump heap memory to a file using jmap ("jmap prints shared object memory maps or heap memory details of a given process or core file or a remote debug server")
jmap -heap:format=b PID (where PID i id of your java process)
- Run jvisualvm
- Load heap.bin file
- Find biggest objects by retained size. After file loads you can see every object that was in memory at the time the dump was taken.Click on find biggest objects by retained size and you will get hierarchy of biggest objects in memory.
In this way, I discovered that I have HashMap object in some other singleton object, and the values were added to map, but never removed.