Java -Xms initial size effects

55,018

Solution 1

The benefit is that there is a performance penalty when you use up enough of the heap that it has to be resized. If you set it initially to 64MB but it turns out your application under load needs 250MB, when you hit near 64MB the JVM will allocate more heap space and possibly move around some objects and do other book-keeping. This of course takes time.

When your application is under load, you want all resources dedicated to making it run, so this extra work can make the application slower to respond, or even in some instances it can crash if it runs out of memory before the heap is resized.

Sometimes when using a Java app, you'll see instructions like "set Xms and Xmx to the same value". This is done to avoid the resizing altogether, so that your application launches with its heap already as big as it will ever be.

Solution 2

The linked article explains it clear enough:

Default values: -Xms 3670k -Xmx 64m [...] Large server applications often experience two problems with these defaults. One is slow startup, because the initial heap is small and must be resized over many major collections. A more pressing problem is that the default maximum heap size is unreasonably small for most server applications. The rules of thumb for server applications are:

  1. Unless you have problems with pauses, try granting as much memory as possible to the virtual machine. The default size (64MB) is often too small.
  2. 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.
  3. In general, increase the memory as you increase the number of processors, since allocation can be parallelized.

You can also be interested in this discussion of the problem.

Solution 3

What is the benefit of setting the -Xms parameter, and having the initial memory larger for example, then the default calculated one

If the initial heap is small and must be resized over many major collections, the startup will be slow.

Also, is there any good to setting both the initial and maximum memories to same size?

Setting -Xms and -Xmx to the same value gives you predictability. This is especially important when sizing the JVM during performance tuning. But the JVM won't be able to compensate any bad decision.

I tend to use the same values for production servers (which are tuned during performance testing).

Solution 4

If it is normal for your application to require more than 64 MB of heap memory, setting Xms to a larger value should improve the application's performance somewhat because the VM would not have to request additional memory as many times.

In a production system I consider setting Xms and Xmx to the same value sensible. It's basically saying "this is the amount of heap memory the VM can get and I'm dedicating it right away".

Share:
55,018
SyBer
Author by

SyBer

Updated on July 31, 2021

Comments