简体   繁体   English

如何确定我的Java程序消耗了多少内存

[英]How can I determine how much memory my java program is consuming

I have a class as follows: 我的课如下:

class MyClass
{
 public static void main(String args[])
 {
  //I want statistics here
 }
}

Any help would be appreciated. 任何帮助,将不胜感激。

Runtime runtime = Runtime.getRuntime(); 运行时运行时= Runtime.getRuntime();

NumberFormat format = NumberFormat.getInstance();

StringBuilder sb = new StringBuilder();
long maxMemory = runtime.maxMemory();
long allocatedMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();

sb.append("free memory: " + format.format(freeMemory / 1024) + "<br/>");
sb.append("allocated memory: " + format.format(allocatedMemory / 1024) + "<br/>");
sb.append("max memory: " + format.format(maxMemory / 1024) + "<br/>");
sb.append("total free memory: " + format.format((freeMemory + (maxMemory - allocatedMemory)) / 1024) + "<br/>");

The MemoryMXBean has all the memory data available. MemoryMXBean具有所有可用的内存数据。 This includes data about both heap and non heap memory. 这包括有关堆和非堆内存的数据。 The Runtime class only provides access to information about heap memory. Runtime类仅提供对堆内存信息的访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM