Is there any advantage in setting Xms and Xmx to the same value?

后端 未结 5 1394
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-08 21:48

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 s

相关标签:
5条回答
  • 2021-02-08 21:55

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

    0 讨论(0)
  • 2021-02-08 22:01
    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.
    0 讨论(0)
  • 2021-02-08 22:03

    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.

    0 讨论(0)
  • 2021-02-08 22:17

    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.

    0 讨论(0)
  • 2021-02-08 22:22

    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

    0 讨论(0)
提交回复
热议问题