eclipse MemoryAnalyzer 看内存泄露

工具下载链接:

http://iso.mirrors.ustc.edu.cn/eclipse/mat/1.7/rcp/MemoryAnalyzer-1.7.0.20170613-win32.win32.x86_64.zip

示例代码:

import java.util.Map;  
  
import java.util.HashMap;  
  
  
/** 
 
 * JVMOutOfMemoryErrorSimulator 
 
 */  
  
public class JVMOutOfMemoryErrorSimulator {  
  
       private final static int NB_ITERATIONS = 500000;  
  
       // ~1 KB data footprint  
  
       private final static String LEAKING_DATA_PREFIX = "datadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadatadata";  
  
       // Map used to stored our leaking String instances  
  
       private static Map<String, String> leakingMap;  
  
       static {  
  
              leakingMap = new HashMap<String, String>();  
  
       }  
  
  
       public static void main(String[] args) {  
  
              System.out.println("JVM OutOfMemoryError Simulator 1.0");  
  
              System.out.println("Author: Pierre-Hugues Charbonneau");  
  
              System.out.println("http://javaeesupportpatterns.blogspot.com/");  
  
              try {  
                     for (int i = 0; i < NB_ITERATIONS; i++) {  
                           String data = LEAKING_DATA_PREFIX + i;  
                      
                           // Add data to our leaking Map data structure...  
  
                           leakingMap.put(data, data);  
                     }  
  
              } catch (Throwable any) {  
  
                     if (any instanceof java.lang.OutOfMemoryError) {  
  
                            System.out.println("OutOfMemoryError triggered! "  
  
                                         + any.getMessage() + " [" + any + "]");  
                     } else {  
  
                           System.out.println("Unexpected Exception! " + any.getMessage()  
  
                                         + " [" + any + "]");  
                     }  
              }  
              System.out.println("simulator done!");  
  
       }  
}  

------------

程序输出:

JVM OutOfMemoryError Simulator 1.0
Author: Pierre-Hugues Charbonneau
http://javaeesupportpatterns.blogspot.com/
java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid15000.hprof ...
Heap dump file created [129086814 bytes in 0.464 secs]
Exception in thread "main" 
Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "main"


dump文件产生在工程目录下:

D:\guolong\eclipsework\testWorkspace\TestProjectjava_pid15000.hprof

step1

导入上述dump文件



step2:点,查看可疑泄露项



step3 找到产生内存泄露的类,并点开详情:


step4:定位产生泄露对象,类中变量的位置






HashMap源码中,table属性的位置:



HashMap源码中,Node结构的定义:

相关文章
相关标签/搜索