13

Usually I set -Xms512m and -Xmx1g so that when JVM starts it allocates 512MB and gradually increases heap to 1GB as necessary. But I see these values set to same say 1g in a dedicated server instance. Is there any advantage for the having both set to the same value?

  • Possible duplicate of [Is it good to set the max and min JVM heap size the same?](http://stackoverflow.com/questions/6862972/is-it-good-to-set-the-max-and-min-jvm-heap-size-the-same) – Greg Dubicki May 10 '17 at 14:55

5 Answers5

19

Well there are couple of things.

  1. Program will start with -Xms value and if the value is lesser it will eventually force GC to occur more frequently
  2. Once the program reaches -Xms heap, jvm request OS for additional memory and eventually grabs -Xmx that requires additional time leading to performance issue, you might as well set it to that at the beginning avoiding jvm to request additional memory.

It is very nicely answered here - https://developer.jboss.org/thread/149559?_sscc=t

slm
  • 12,534
  • 12
  • 87
  • 106
Fairoz
  • 1,345
  • 9
  • 15
  • 5
    I'm not sure that a post from 2010 is a valid source today.. A lot has happened since then. – Tobb Oct 30 '17 at 09:31
11

From Oracle Java SE 8 docs:

https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/sizing.html By default, the virtual machine grows or shrinks the heap at each collection to try to keep the proportion of free space to live objects at each collection within a specific range. This target range is set as a percentage by the parameters -XX:MinHeapFreeRatio=<minimum> and -XX:MaxHeapFreeRatio=<maximum>, and the total size is bounded below by -Xms<min> and above by -Xmx<max>. 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.

if the value of -Xms and -Xmx is same JVM will not have to adjust the heap size and that means less work by JVM and more time to your application. but if the chosen value is a poor choice for -Xms then some of the memory allocated will never be used because the heap will never shrink and if it is a poor choice for -Xmx you will get OutOfMemoryError.

Suraj Rao
  • 28,186
  • 10
  • 88
  • 94
Dev
  • 312
  • 2
  • 6
7

There are some advantages.

  • if you know the size is going to grow to the maximum, e.g. in a benchmark, you may as well start with the size you know you need.
  • you can get better performance giving the program more memory that it might naturally give itself. YMWV

In general, I would make the Xms a value I am confident it will use, and the double this for head room for future use cases or situations we haven't tested for. i.e. a size we don't expect but it might use.

In short, the maximum is the point you would rather the program fail than use any more.

Paulo Fidalgo
  • 19,844
  • 7
  • 85
  • 108
Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075
7

AFAIK One more reason, is that expansion of heap is a stop-the-world event; setting those to the same value will prevent that.

Eugene
  • 102,901
  • 10
  • 149
  • 252
1
  1. Application will suffer frequent GC with lower -Xms value.
  2. Every time asking for more memory from OS with consume time.
  3. Above all, if your application is performance critical then you would certainly want to avoid memory pages swapping out to/from disk as this will cause GC consuming more time. To avoid this, memory can be locked. But if Xms and Xmx are not same then memory allocated after initial allocation will not be locked.
Dexter
  • 88
  • 7