What is the exact difference between Runtime.maxMemory()
and Runtime.totalMemory()
? The javadoc is quite vague about this (for me).
What are typical use cases for these two methods, that is, When would it be inappropriate to use the respective other one?
The totalMemory()
returns how much memory is currently used, while the maxMemory()
tells how much the JVM can allocate in total.
Note: that from this follows: totalMemory() <= maxMemory()
, and you can also get 'how much memory is left' by maxMemory() - totalMemory()
One use case for this is to diagnose how much memory your program uses, and you'll use totalMemory()
for it.
Note: both are referring only to heap memory, and not stack memory.
The total memory is the memory that is currently allocated to the JVM. It varies over time. The max memory is the maximum memory that the JVM could ever reach. It's the upper limit of the total memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With