How to understand java.lang.management.MemoryMXBean and -Xms?

偶尔善良 提交于 2020-06-27 15:22:48

问题


MemoryMXBean.getHeapMemoryUsage()

I have a jvm process that ran with -Xms512m -Xmx512m, and below show the MemoryMXBean.getHeapMemoryUsage() of this process:

    init     = 512M
    used     = 105M
    comitted = 491M
    max      = 491M
  1. Why max is 491M(I expect it to be 512M)?

MemoryMXBean.getNonHeapMemoryUsage() MemoryMXBean.getNonHeapMemoryUsage() of this process:

    init     = 2M
    used     = 25M
    comitted = 25M
    max      = 0M
  1. What does the non-heap(who uses it) means? What kind of memory will be count into this non-heap? I only know that the direct memory we used in java code wouldn't(Am I right?)

-Xms

  1. What does -Xms(The initial heap size) mean? I used to think that the initial heap size is how much memory jvm will actually allocate from os when the jvm start, but it turns out to be wrong. top shows that RES of this jvm is nearly 150m, but the jvm was ran with -Xms512M.

  2. Is the below formula correct(or almost correct-_-)? If not, what should be considered too?

total memory a jvm used = used of MemoryMXBean.getHeapMemoryUsage() 
+ used of MemoryMXBean.getNonHeapMemoryUsage()
+ the direct memory we used in application level

Anything would be appreciated!


回答1:


Why max is 491M(I expect it to be 512M)?

One survivor space is not counted in max, because one of survivor spaces is always empty.
See also this answer.

What does the non-heap(who uses it) means?

MemoryMXBean counts the following JVM memory pools as "Non-heap":

  • Code Cache (or Code Heap) - the area for compiled methods and other dynamically generated code;
  • Metaspace and Compressed Class Space - the areas for class metadata.

See also this question.

What does -Xms(The initial heap size) mean?

Yes, it's the initial heap size. OS allocates pages in physical memory lazily (on the first access). That's why RSS can be smaller than total committed size.

See this answer for details.

Is the below formula correct

No. Things are much more complicated. I've explained this in detail in this answer.



来源:https://stackoverflow.com/questions/54210134/how-to-understand-java-lang-management-memorymxbean-and-xms

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