Java heap dump and the heap size after the heap analysis differs

感情迁移 提交于 2019-12-01 15:04:52
Nikem

top and other OS level tools show how much system memory does your JVM process consume. Java heap, defined by -Xmx command line option, is only a part of that memory. Apart from heap JVM needs some memory for itself. Then there are java threads, each requiring a certain amount of memory. And Metaspace/Permanent Generation. And several others. You can read this blog post and this SO answer for more information.

About the size of the dump file and the actual heap size the answer of @arnab-biswas is certainly true. MAT reports the size of actually used heap, consumed by live objects. But heap dump contains the whole of the heap, including garbage.

I have experienced similar situation. The difference (HPROF file size - Size of the heap indicated by MAT) is effectively garbage (unreachable objects). Unreachable object Histogram in MAT should help here.

jmap -F -dump:live,format=b,file=<file_name.hprof> <process_id> will only dump live objects and NOT garbages.

In order to monitor the native memory you need to start your application with -XX:NativeMemoryTracking=summary or -XX:NativeMemoryTracking=detail. Note that there is a performance penalty, so think twice before doing it in production.

When memory tracking is active you can use jcmd <pid> VM.native_memory summary. There are other commands available as well, check https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr007.html or search for native memory tracking.

EDIT: I didn't follow the links before answering, you may be looking for something like https://github.com/jeffgriffith/native-jvm-leaks instead.

Terry Li

You are asking for an answer drawing from credible/official sources. Let me give it a try.

1) why is the memory consumed by my JVM process (shown by Top) larger than the heap size?

Because the total memory consumption of the JVM process consists of more things than just the Java heap. A few examples:

  • Generated (JIT:ed) code
  • Loaded libraries (including jar and class files)
  • Control structures for the java heap
  • Thread Stacks
  • User native memory (malloc:ed in JNI)

Credible/official sources: Run-Time Data Areas and this blog post

2) why is the heap dump size much bigger than what MAT reports?

Because MAT does not show the complete heap. During the index creation, the Memory Analyzer removes unreachable objects because the various garbage collector algorithms tend to leave some garbage behind.

Credible/official sources: MemoryAnalyzer/FAQ

Heap dump : A heap dump is a snapshot of the memory of a Java process at a certain point of time. There are different formats for persisting this data, and depending on the format it may contain different pieces of information, but in general the snapshot contains information about the java objects and classes in the heap at the moment the snapshot was triggered. Usually a full GC is triggered before the heap dump is written so it contains information about the remaining objects.

For information related to MAT found here http://help.eclipse.org/neon/index.jsp?topic=/org.eclipse.mat.ui.help/welcome.html

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