2013-06-21 28 views
1

用我發現的以下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); 
    } 
} 

回答

0

所以方法本身不佔用在JVM內存那就是你自己創建只會造成你的編譯的類更大的足跡方法的方法等中創建的對象。

您可以嘗試使用諸如VisualVMJProfiler之類的內容進行分析。嘗試在Debug中設置斷點並在您逐步瀏覽程序時觀察這些分析器中的內存,並注意所謂的「問題區域」。

這些剖析會給你很多的性能統計數據來給你

0

多年前等需要記憶的總體思路,我打了,可以測量特定對象的深存儲器使用Classmexer劑。 http://www.javamex.com/tutorials/memory/instrumentation.shtml

恕我直言,對於大多數情況下,使用Java Profiler獲取大圖片使用情況會更有用。我想,除了好玩之外,Classmexer的一個用例就是如果你確實需要在堆中存儲大量實例,就要進行大小調整。

相關問題