用我發現的以下Class here我可以得到關於虛擬機內存的一些統計信息。我的java程序最多使用多少內存?
但是,還有一種方法來獲得我的程序或我的程序的特定方法所需的最大內存?
/**
* Class: TestMemory
* @author: Viral Patel
* @description: Prints JVM memory utilization statistics
*/
public class TestMemory {
public static void main(String [] args) {
int mb = 1024*1024;
//Getting the runtime reference from system
Runtime runtime = Runtime.getRuntime();
System.out.println("##### Heap utilization statistics [MB] #####");
//Print used memory
System.out.println("Used Memory:"
+ (runtime.totalMemory() - runtime.freeMemory())/mb);
//Print free memory
System.out.println("Free Memory:"
+ runtime.freeMemory()/mb);
//Print total available memory
System.out.println("Total Memory:" + runtime.totalMemory()/mb);
//Print Maximum available memory
System.out.println("Max Memory:" + runtime.maxMemory()/mb);
}
}