Srii
Srii

Reputation: 587

JVM Option: Xmx Allocation

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.

Upvotes: 3

Views: 5157

Answers (4)

parsifal
parsifal

Reputation: 451

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).

Upvotes: 3

Andrew Logvinov
Andrew Logvinov

Reputation: 21831

JVM initially allocates amount of memory that is specified via -Xms option (default is 32M, as far as I know). When it runs out of this memory, it will start allocating new memory incrementally until it reaches the amount specified via -Xmx option (default is 64M).

When this happens, OutOfMemory error is thrown.

P.S. Of course, this is a quite simplified algorithm. In fact, JVM uses a much more complex one.

Upvotes: 1

Adam Dyga
Adam Dyga

Reputation: 8896

-Xmx parameter specifies the maximum memory JVM can use, but it doesn't allocate it from the OS immediately after start-up. It does it incrementally, usually in many steps, as it needs to do so (ie. the Java application in JVM demands more memory as it runs).

Of course this is the default behavior, it can be changed with many other JVM params.

Upvotes: 1

krico
krico

Reputation: 5728

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.

Upvotes: 1

Related Questions