Reputation: 11269
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.
Upvotes: 0
Views: 350
Reputation: 11269
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.
Upvotes: 0
Reputation: 70564
I'd execute the program with plenty of heap while monitoring heap usage with JConsole. Take note of the highest memory use after a major garbage collection, and set about maximum heap size 50% to 100% higher than that amount to avoid frequent garbage collection.
As an aside, totalMemory reports the size of the heap, not how much of it is presently used. If you set minimum and maximum heap size to the same number, totalMemory will be the same irrespective of what your program does ...
Upvotes: 1