0

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.

2
  • "my execution times are typically 10 hours or more" then don't set Xms and let the JVM optimise the heap size for you: it's very good at it... Commented Sep 21, 2013 at 9:21
  • And then what? Report the highwater level of .totalMemory() on a periodic basis, of say 1 second, and use that number to set Xmx? Commented Sep 21, 2013 at 9:25

2 Answers 2

1

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 ...

Sign up to request clarification or add additional context in comments.

2 Comments

In my experiments .totalMemory() is more variable than .maxMemory() which is why I took it to be the amount of heap used. If as you suggest, totalMemory is not the heap used, then it would seem that neither is maxMemory which tended not to vary.
I'm quite sure I am right, as the javadoc of Runtime.totalMemory says: "returns the total amount of memory currently available for current and future objects, measured in bytes". Since future objects are included, this must include presently unused areas of the heap.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.