Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JVM Option: Xmx Allocation

Tags:

java

jvm

Although, the question is very basic, I am willing to understand how JVM's maximum memory allocation is done to an application. I have an application running on a Windows 2008 server that hosts about 60 virtual managed servers [that is 60 JVMs]. Each managed server is set a maximum heap of 1024m. The Windows is configured with a 32 GB RAM.

Now the question, how is maximum memory allocation to a JVM done? Is it done at one go or on a incremental growth basis? If at one go, how does Windows handle all 60 managed servers hosted my application in a 32-GB-RAM-packed system?

Any views are much appreciated. Thank you.

like image 637
Srii Avatar asked Nov 21 '25 19:11

Srii


2 Answers

The JVM actually does allocate all memory requested by -Xmx at startup time, along with additional memory to hold the JVM executable and internal work areas. And when you create threads, it also allocates memory for the stacks used for those threads.

This works because (1) the JVM doesn't actually use this memory, and (2) the OS provides a paging file. When the JVM requests an allocation, it creates a commitment by the OS but does not actually use all RAM requested. As it actually uses the RAM, the OS will swap pages to/from the pagefile. If all processes were actively using their RAM, the OS would have to swap pages in and out constantly; this is called "thrashing."

The -Xms parameter specifies the initial heap size, within the overall heap size. The JVM will attempt to keep memory within these bounds, but is permitted to expand the bounds if it cannot reclaim enough garbage. However, these heap increases do not incrementally request more memory from the OS. If they did, the heap would be fragmented and a large array allocation might fail (because it wouldn't fit in contiguous memory).

like image 140
parsifal Avatar answered Nov 24 '25 10:11

parsifal


If you want it to be allocated whenver you start up the process, you need to supply -Xms, if you only supply -Xmx the VM will grow up to that value but only increment whenever it needs more memory.

like image 45
krico Avatar answered Nov 24 '25 09:11

krico



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!