Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the JVM consume less memory than -Xms specified?

My question is as the title, and I got some knowledge by searching:

  1. linux has shared memory How to measure actual memory usage of an application or process?

  2. JVM will reserve the amount of memory setted in Xms What does the -Xms JVM mean in reference to heap memory?

But still don't know why, some on can offer some help?

Here is my test run on Ubuntu12.04(64bit) | JDK 1.7.0_04. and top shows below:

  PID USER      PR  NI  VIRT  RES       SHR S %CPU %MEM    TIME+  COMMAND
 4067 brian     20   0 5316m **262m** 7496 S    0  3.3   0:00.30 java -**Xms4096m** -Xmx4096m Test
 4066 brian     20   0 3182m **256m** 7496 S    0  3.3   0:00.30 java -**Xms2048m** -Xmx2048m Test
 4065 brian     20   0 2114m **252m** 7492 S    0  3.2   0:00.30 java -**Xms1024m** -Xmx1024m Test
 4064 brian     20   0 1314m  **76m** 7584 S    0  1.0   0:00.20 java -**Xms256m** -Xmx256m Test
 4063 brian     20   0 1180m  **51m** 7608 S    0  0.7   0:00.21 java -**Xms128m** -Xmx128m Test
like image 447
Brian HU Avatar asked Aug 24 '12 11:08

Brian HU


People also ask

Why does the JVM consume more memory than the amount given to XMX?

Java Virtual Machine optimizes the code during runtime. Again, to know which parts to optimize it needs to keep track of the execution of certain code parts. So again, you are going to lose memory.

Why should Xms and XMX be the same?

Setting -Xms and -Xmx to the same value increases predictability by removing the most important sizing decision from the virtual machine. However, the virtual machine is then unable to compensate if you make a poor choice.

Why JVM heap utilization is too high?

This is because the JVM steadily increases heap usage percentage until the garbage collection process frees up memory again. High heap usage occurs when the garbage collection process cannot keep up. An indicator of high heap usage is when the garbage collection is incapable of reducing the heap usage to around 30%.

Can JVM use more than XMX?

Therefore, JVM memory usage can be more than the -Xmx value under peak business load.


2 Answers

You're looking at the resident memory - that is, the physical RAM consumed. See here for more info.

The virtual memory, however, is the memory consumed by your application, including the memory swapped out (to disk). You'll see there's a closer correspondance with the virtual memory and your -Xms settings.

See this ServerFault answer for more info.

like image 99
Brian Agnew Avatar answered Sep 28 '22 05:09

Brian Agnew


When your JVM starts it reserves your maximum heap size as one continuous block of virtual memory on startup. However, only the pages actually used become resident pages (actual main memory)

When you set the minimum size, it doesn't force the JVM to use that much memory if it doesn't need it. Instead it takes little effort to reduce memory up to this point (you might still see some minor collections) In most applications the minimum size is reached almost immediately, but a "hello world" program will use the same memory no matter how large you set the minimum size to be.

like image 37
Peter Lawrey Avatar answered Sep 28 '22 04:09

Peter Lawrey