a question on Runtime.getRuntime().totalMemory()
The following function is used to compute 开发者_如何学JAVAmemory 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()
?
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.)
精彩评论