Reputation: 8653
I understand that, -Xms
and -Xmx
are used for specifying minimum and maximum heap size
for java program.
And as per my understanding, JVM can work if we don't specify any of the options, i.e heap size can be managed by JVM itself.
So, if we want to limit maximum heap size, we should use -Xmx
and that is OK, now my question is, why anybody needs to limit minimum size of heap, as it can be very well managed by JVM.
Why -Xms
is required at all?
Upvotes: 6
Views: 2734
Reputation: 4057
You can reduce garbage collection cycles by setting a larger minimum heap size. This doesn't make a huge difference on long running programs, but short running stuff can benefit from this.
The JVM sets the initial heap size to some amount of the maximum heap size and then grows the heap and performs (mostly minor) GC runs when the used memory exceeds some threshold. So, when setting an initial heap size with -Xms
(which is then immediately allocated on JVM startup) you can avoid these heap grow cycles.
Upvotes: 13
Reputation: 68715
JVM memory grows as required between the range of -Xms and -Xmx parameters. The difference between -Xms and -Xmx apart from being minimum and maximum heap size ranges is that; -Xms amount of memory if available will be assigned immediately by OS to your application but -Xmx will be treated like a request taken and will be served as and when required upto that limit.
Every application needs some minimum amount of memory to boot up and work in normal scenarios. If an application developer is able to assess that then specifying -Xms makes sense. Having the sufficient required memory upfront may reduce the overhead of growing the heap from a value to other.
Upvotes: 0