30

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.

4 Answers 4

33

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.

4
  • 1
    From your experience, is it recommended to just set the Xms to same value as Xmx?
    – SyBer
    Commented Mar 28, 2010 at 9:10
  • 7
    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
    Commented Mar 28, 2010 at 9:21
  • 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.
    – Jé Queue
    Commented Apr 21, 2010 at 23:57
  • 2
    @Phil, This is a great answer, but it would be better if your comment in response to SyBer's question were folded into the original answer.
    – Mark Booth
    Commented Jul 12, 2011 at 10:49
6

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.

4

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

1
  • Can you provide your thoughts on stackoverflow.com/questions/39652282/… and stackoverflow.com/questions/39652282/… ? Thanks in advance
    – emilly
    Commented Sep 23, 2016 at 5:42
3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.