MemoryAnalizer它是一個Eclipse推出的記憶體分析工具(Memory Analizer Tool)簡稱MAT我們可以通過它的協助找出記憶體流失,並且減少記憶體的消耗. 要想使用它首先要在Eclipse中裝上該外掛程式,步驟如下: 1>,首先下載外掛程式,地址如下:http://www.eclipse.org/mat/downloads.php
2> 2>,下載完成後可以在Eclipse中安裝
public class Main {/** * @param args */public static void main(String[] args) {List<String> list = new ArrayList<String>();while (1<2){list.add("OutOfMemoryError soon");}}}
然後單擊右鍵 --> Run Configurations彈出如下對話方塊,把紅色標註的內容添上去.-XX:+HeapDumpOnOutOfMemoryError
然後單擊Run,運行完成後,控制台出現如下資訊:
然後重新整理工程發現出現一個以hprof結尾的檔案,如下所示:
然後通過外掛程式將其開啟,但是我們的外掛程式不知道為什麼打不開,總是報錯,最後在網上找答案,建議使用RCP(Rich Client Platform,胖用戶端平台)可以到官網上去下載,http://www.eclipse.org/mat/downloads.php下載成功後解壓,開啟MemoryAnalyzer.exe檔案
然後通過該工具開啟我們產生的hprof檔案 更詳細內容參考:http://wiki.eclipse.org/index.php/MemoryAnalyzer#Getting_Startedhttp://www.blogjava.net/rosen/archive/2010/06/13/323522.html
在android使用MAT外掛程式: Dalvik虛擬機器在啟動並執行的時候,也有記憶體回收機制,但是這不意味著我們可以忽略記憶體的管理,我們應該更加關心手機裝置的記憶體的使用,因為手機的記憶體比較緊張.在這篇文章裡面,我們將要看看一些在android SDK中的記憶體描繪的工具,協助我們調整應用程式記憶體的使用. 一些記憶體的使用問題是比較明顯的,例如,你的應用程式在使用者每次觸控螢幕幕都出現記憶體流失,這可能觸發OutofMemoryError異常,並且程式崩潰了.其他的記憶體流失問題比較微妙了,可能僅僅導致應用程式(記憶體回收次數更加頻繁並且花費時間更長)和系統執行效率下降, Android SDK提供了兩種主要的方式來描述程式記憶體的使用:Allocation Tracke(in DDMS[Dalvik Debug Monitor Service])和 heap dumps.
當我們想擷取什麼類型的分配將會發生在一個給定的時間段內,但是他不會給我們任何關於應用程式堆的所有狀態資訊.更多關於Allocation Traker的資訊請查看 http://developer.android.com/resources/articles/track-mem.html接下來我們將把焦點放在heap dumps上面,它是一個更加強大的記憶體分析工具. 一個heap dumps是一個應用程式堆的快照,它被存放在一個格式叫HPORF的二進位檔案裡,Dalvik使用這個格式是類似的,但是不同於HPROF
tool in Java.這裡有一些產生一個正在啟動並執行android程式的heap dumps.一種是使用Dump HPROF file 按鈕在DDMS裡
如果我們想更加精確的知道什麼時候dump被建立,我們能夠使用android .os.Debug.dumpHprofData()方法以編程的方式來建立deap dump. 分析一個heap dump,可以使用標準的工具像Jhat(http://docs.oracle.com/javase/6/docs/technotes/tools/share/jhat.html)或者MAT.然後,第一步需要轉換.hprof檔案格式從Dalvik格式到Javase HPROF格式.可以使用Android SDK中的hprof-conv工具,在cmd命令列裡輸入該命令,使用格式如下
hprof-conv in.hprof out.hprof
下面通過一個例子來講解:先建立一個Activitypublic class MainActivity extends Activity {
static ArrayList<Pilot> list = new ArrayList<Pilot>();//定義一個集合,如果在實際程式可能就是當作緩衝了. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); for (int i = 0; i < 2000; i++)
{//在裡面添加2000個對象. list.add(new Pilot("dsfdsgdgdgvfgdsfsd",i)); } }
}然後啟動該程式,在DDMS中Device 面板裡面可以看到該程式的進程.
介紹一下這幾個按鈕的作用:
單擊我們程式的進程,然後單擊 update heap按鈕,右邊的面板可能會出現如所示的內容,如果不出現單擊Cause GC.
在此基礎上建立一個heap dump,在Devices工具列裡面有一個Dump HPROF file 按鈕,單擊該按鈕就會彈出hprof檔案儲存的路徑(在此之前要保證我們選中的是我們程式的進程),然後選擇一個路徑儲存即可.接著通過hprof-conver命令轉換一下格式即可.最後我們通過安裝的外掛程式或者RCP開啟即可.我這裡是用過RCP開啟的.:
我們還可以開啟Histogram View,所在位置:
開啟結果如所示:
我們在Activty建立了2000個Pilot對象在這裡都有顯示.並且還支援對象個數,shallow heap(表示所有對象佔用的記憶體大小),retained heap(保持這些對象存活,還包括其他對象對它們引用所需要的記憶體.)的排序列表. 調試記憶體流失,有時候是有用的在一個不同的時間點去比較堆的狀態,這需要我們建立兩份HPROF檔案,那麼如何去比較兩個heap dumps在MAT裡(這有一點點複雜)?1>開啟第一個HPROF檔案(單擊file-->Open Heap Dump)2>開啟Histogram View3>....原文http://android-developers.blogspot.com/2011/03/memory-analysis-for-android.html:
- Open the first HPROF file (using File > Open Heap Dump).
- Open the Histogram view.
- In the Navigation History view (use Window > Navigation History if it's not visible), right click on histogram and selectAdd to Compare Basket.
- Open the second HPROF file and repeat steps 2 and 3.
- Switch to the Compare Basket view, and click Compare the Results (the red "!" icon in the top right corner of the view).
Conclusion
In this article, I've shown how the Allocation Tracker and heap dumps can give you get a better sense of your application's memory usage. I also showed how The Eclipse Memory Analyzer (MAT) can help you track down memory leaks in
your app. MAT is a powerful tool, and I've only scratched the surface of what you can do with it. If you'd like to learn more, I recommend reading some of these articles:
- Memory Analyzer News: The official blog of the Eclipse MAT project
- Markus Kohler's Java Performance blog has many helpful articles, including Analysing the Memory Usage of Android
Applications with the Eclipse Memory Analyzer and 10 Useful Tips for the Eclipse Memory Analyzer.
歡迎轉載:http://blog.csdn.net/johnny901114/article/details/7823219