The following function is used to compute memory usage.
private static long getMemoryUse(){
putOutTheGarbage();
long totalMemory = Runtime.getRuntime().total
In specific, how to understand the relationships connecting "Runtime", "getRuntime()" and "totalMemory()"
Runtime
is a class.
getRuntime()
is a static method of Runtime
that returns a (actually THE ONLY) instance of Runtime
.
totalMemory()
is an instance method of Runtime
that returns the "total memory" used.
For more details, read the javadoc.
Note that the definitions of the values returned by freeMemory
, totalMemory
and maxMemory
are rather vague. Furthermore, the first two do not return the memory usage at the instant the method is called. Instead, they typically return a value calculated last time the GC ran.
(One reason for the vaguely worded API specs is to avoid making these methods too costly, and/or too restrictive on the JVM implementation. It would be a BAD THING if the JVM couldn't use some fast GC technology because of a javadoc requirement to return accurate values for the memory usage at all times.)