4

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!

winflex
  • 113
  • 7

1 Answers1

5

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.

apangin
  • 92,924
  • 10
  • 193
  • 247