打開eclipse >> Help >> Install New Software >> 點擊Work With右邊的Add,輸入java
[Name] MAT [Location] http://archive.eclipse.org/mat/1.2/update-site/
點擊OK >> 點擊Install 窗口下的Select All(選擇所有), 而後一直點 下一步 直到結束便可。eclipse
建立java項目(只需一個類),代碼(類名OOMObject)ide
1 import java.util.ArrayList; 2 import java.util.List; 3 4 /** 5 * Created by foreverenjoy on 16-7-9. 6 */ 7 public class HeapOOM { 8 static class OOMObject { 9 10 } 11 12 public static void main(String[] args) { 13 List<OOMObject> list = new ArrayList<>(); 14 15 while (true) { 16 list.add(new OOMObject()); 17 } 18 } 19 }
在Debug Cofigurations中設置VM參數:spa
-Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryErrordebug
經過參數-XX:+HeapDumpOnOutOfMemoryError能夠讓虛擬機在出現內存溢出異常時Dump出當前的內存堆轉存快照以便過後分析。code
點擊debug運行項目,會出現blog
java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid17179.hprof ...
Heap dump file created [27535550 bytes in 0.099 secs]

java_pid17179.hprof(虛擬機Dump出的內存堆轉存快照)文件能夠用Memory Analyzer Tool進行分析。
點擊File >> Open File >> 選中本Java項目下的java_pid17179.hprof(java_*.hprof) >> 選中Leak Suspecs Report >> Finish
如下就是Memory Analyzer的分析結果 ip