a question on Runtime.getRuntime().totalMemory()

前端 未结 1 1831
借酒劲吻你
借酒劲吻你 2021-01-23 07:08

The following function is used to compute memory usage.

private static long getMemoryUse(){
  putOutTheGarbage();
  long totalMemory = Runtime.getRuntime().total         


        
相关标签:
1条回答
  • 2021-01-23 07:25

    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.)

    0 讨论(0)
提交回复
热议问题