The following function is used to compute memory usage.
private static long getMemoryUse(){
putOutTheGarbage();
long totalMemory = Runtime.getRuntime().totalMemory();
putOutTheGarbage();
long freeMemory = Runtime.getRuntime().freeMemory();
return (totalMemory - freeMemory);
}
I do not understand how to understand Runtime.getRuntime().totalMemory()? In specific, how to understand the relationships connecting Runtime, getRuntime() and totalMemory()?
Runtimeis a class.getRuntime()is a static method ofRuntimethat returns a (actually THE ONLY) instance ofRuntime.totalMemory()is an instance method ofRuntimethat returns the “total memory” used.For more details, read the javadoc.
Note that the definitions of the values returned by
freeMemory,totalMemoryandmaxMemoryare 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.)