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

蓝咒 提交于 2020-02-21 13:28:50

问题


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?


回答1:


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




回答2:


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.




回答3:


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




回答4:


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.




回答5:


  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.


来源:https://stackoverflow.com/questions/43651167/is-there-any-advantage-in-setting-xms-and-xmx-to-the-same-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!