How do I determine the lower bound for the JVM option Xmx
or otherwise economize on memory without a trial and error process? I happen to set Xms
and Xmx
to be the same amount, which I assume helps to economize on execution time. If I set Xmx
to 7G, and likewise Xms
, it will happily report that all of it is being used. I use the following query:
Runtime.getRuntime().totalMemory()
If I set it to less than that, say 5GB, likewise all of it will be used. It is not until I provide very much less, say 1GB will there be an out-of-heap exception. Since my execution times are typically 10 hours or more, I need to avoid trial and error processes.
Using Xms256M
and Xmx512M
, and a trivial program, freeMemory
is 244M and totalMemory
is 245M and maxMemory
is 455M. Using Xms512M
and Xmx512M
, the amounts are 488M, 490M, and 490M. This suggests that totalMemory
is a variable amount that can vary if Xms
is less than Xmx
. That suggests the answer to the question is to set Xms
to a small amount and monitor the highwater mark of totalMemory
. It also suggests maxMemory
is the ultimate heap size that cannot be exceed by the total of current and future objects.
Once the highwater mark is known, set Xmx
to be somewhat more than that to be prudent -- but not excessively more because this is an economization effort -- and set Xms
to be the same amount to get the time efficiency that is evidently preferred.