4
votes

While reading some notes on performance tuning, I did notice a recommendations while setting memory size:

Java application should size both initial and maximum permanent generation size to the same value since growing or contracting the permanent generation space requires a full GC. Similar suggestion is given while setting the heap size, I.e. -Xmx=-Xms.

My question is, then why do we have -Xms setting at all?

Also, Why GC gets triggered often if I''ve different value for -Xmx and -Xms, and not when I''ve same size for -Xmx and -Xms.

To add more to my second question, If I start with minimum heap size of 64M and Max 512 M, I believe full GC will not get triggered unless memory utilized by my app reaches 512M.

Similarly If I start with 512M for both -Xmx and -Xms, still JVM will trigger full GC when my app memory use reaches this limit. So why it's advised to set both max and min to the same value?

3

3 Answers

2
votes

The setting flags were designed before the VM had generational, incremental collection. In that case complete collections were all there were. In more modern collectors full collections are rare. That's good because incremental collections are normally a few milliseconds, so the UI experience doesn't change. Complete collections of big arenas can take several seconds ore more. Changing the arena size - as the document says - is guarenteed to cause a full collection every time.

The guidance isn't perfect 100% of the time. There are few kinds of apps where allowing the arena to grow is reasonable.

1
votes

-Xms=64m -Xmx=512m does not mean "start up with a heap between 64 and 512 MB". It instructs the JVM to request 64MB of committed memory and 512MB of reserved memory at startup. The heap will be 64MB to start with, and as it fills up, will expand into the space reserved for it. So, with Xms of 64MB you would see a full collection before the heap filled to 64MB.

If you start your application with a low value for Xms and turn on GC logging (-verbose:gc -Xloggc:FILENAME) the log file will show how the heap and generation sizes change as the application runs.

Minor collections may be more frequent with a lower Xms because the new generation will be smaller (assuming you are using proportional generation sizing rather than explicit) and so will fill more quickly.

0
votes

One reason to have -Xms < -Xmx is to allow the JVM to not pre-allocate the whole Xmx upfront so that the difference is available (foe a while perhaps) to other applications. Gory details here: http://www.ibm.com/developerworks/library/j-memusage/