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 themaxMemory()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’ bymaxMemory() - 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.