views:

540

answers:

4

Hi.

What is the benefit of setting the -Xms parameter, and having the initial memory larger for example, then the default calculated one (64 MB in my case, according to Java GC tunning: http://java.sun.com/javase/technologies/hotspot/gc/gc_tuning_6.html#par_gc.ergonomics.default_size)?

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

Thanks.

+3  A: 

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.

Phil
From your experience, is it recommended to just set the Xms to same value as Xmx?
SyBer
It depends on the application. If you are a financial institution with a dedicated server for an app or a set of apps, it makes sense. Especially since an app dealing with market orders can potentially be handling 100's or 1000's of messages/second, with average latency of 2-25ms. A resize could make that latency 5000% worse, for all of the orders sent in at that time. You could even drop orders.As always, though, profile before you attempt to optimize.
Phil
It should be noted that for systems with large number of active JVMs -Xms will increase average JVM footprint, whereas allowing the JVM to self-manage its heap spread allows one to employ memory footprint collection schemes.
Xepoch
+3  A: 

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.

Roman
+1  A: 

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

Tomislav Nakic-Alfirevic
+2  A: 

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

Pascal Thivent