---
title: "使用 Eclipse Memory Analyzer 检测内存泄漏问题 "
author: "alwarse"
date: "2012-09-18T15:03:23Z"
tags: ["MAT"]
categories: ["编程语言"]
slug: "eclipse-memory-analyzer"
draft: false
lastmod: "2012-09-18T15:03:23Z"
id: 1679741
url: "https://alwarse.iteye.com/blog/1679741"
---

本文是关于在开发Talend RCP 过程中碰到一个内存溢出问题的解决方案。使用的检测软件为Eclipse Memory Analyzer ([http://www.eclipse.org/mat/](http://www.eclipse.org/mat/))，是一个开源免费的内存分析工具，目前为eclipse的孵化项目，也是一个eclipse RCP.

Talend一款开源的ETL软件，提供数据集成服务。基于eclipse RCP，EMF, GEF，JET等技术开发。

 

在Talend软件系统中，一个designer editor 是一个mulit page editor, 它的实例是MultiPageTalendEditor，其中包含两页，第一页是一个gef Editor，用来显示GEF图形；第二页是一个JavaEditor,用来显示生成的代码。如下两图所示：

 

![](http://p.blog.csdn.net/images/p_blog_csdn_net/moneyice/p1.PNG) 

![](http://p.blog.csdn.net/images/p_blog_csdn_net/moneyice/p2.PNG)

 

 

 

 

    经过其他工具的测试发现JavaEditor如果用在mulit page editor中，关闭后，起实例无法回收，造成内存泄漏，因为Editor是一个非常频繁使用的功能项，所以该问题非常严重。

 

 

要使用Eclipse Memory Analyzer，首先要生成dump文件。

在eclipse中配置 Preferences -&gt; Java -&gt; Installed JREs, 定义Jdk1.6来运行Talend.

Talend运行后，执行一下打开editor的操作，在将editor关闭，这样虚拟机中保存了相应的堆栈信息。其实也就是执行一下你认为会产生内存泄漏的操作。

 

运行jdk1.6 bin 目录下的jconsole.exe，运行后如下图所示，选择连接到运行的Talend。

 

  ![](http://p.blog.csdn.net/images/p_blog_csdn_net/moneyice/p3.PNG)

 

 

 

选择 Mbean -&gt; com.sun.management -&gt; HotSpotDiagnos -&gt; Operations -&gt; dumpHeap

在参数p0中填写保存dump文件的路径，例如f:/my.hprof, 文件的后缀名要为hprof。

点击dumpHeap按钮生成dump文件。

 

 ![](http://p.blog.csdn.net/images/p_blog_csdn_net/moneyice/p8.PNG)

 

 

 

打开Eclipse Memory Analyzer, 在菜单File 下，选择打开刚才生成的dump文件。如下图：

 

 ![](http://p.blog.csdn.net/images/p_blog_csdn_net/moneyice/p4.PNG)

 

 

 

 

在第一行的filter中填写需要检查的类名，这里我要检查的类为TalendJavaEditor。在如下图所示的上下文菜单中选择 List objects -&gt; with incoming references, 查看保存了TalendJavaEditor 实例的引用。

 

![](http://p.blog.csdn.net/images/p_blog_csdn_net/moneyice/p5.PNG) 

 

 

 

 

在如下图所示的上下文菜单中选择 Path To GC Roots -&gt; exclude weak/soft references, 过滤掉弱引用，因为在这里弱引用不是引起问题的关键。

 

![](http://p.blog.csdn.net/images/p_blog_csdn_net/moneyice/p6.PNG) 

 

 

 

 

从下图中，可以看到JavaInfomationProvider中保存了TalendJavaEditor的引用。所以打开JavaInfomationProvider的代码，进行检查。从下图的分析中，大概可以看出这是一个注册了的监听器没有反注册的问题，这也是引起内存泄露的原因中最常见的。

 

 ![](http://p.blog.csdn.net/images/p_blog_csdn_net/moneyice/p7.PNG)

 

 

 

**public** **JavaInformationProvider**(IEditorPart editor) {

 

       fEditor= editor;

 

       **if** (fEditor != **null**) {

 

           fPartListener= **new** **EditorWatcher**();

           IWorkbenchWindow window= fEditor.**getSite**().**getWorkbenchWindow**();

           window.**getPartService**().**addPartListener**(fPartListener);

 

           **update**();

       }

    }

 

在**JavaInformationProvider 的构造方法中，保存了TalendJavaEditor的实例，并且注册了监听器。**

 

 

 

 

**public** **void** **partClosed**(IWorkbenchPart part) {

           **if** (part == fEditor) {

               fEditor.**getSite**().**getWorkbenchWindow**().**getPartService**().**removePartListener**(fPartListener);

              fPartListener= **null**;

           }

       }

 

 

EditorWatcher的方法**partClosed中，当关闭一个designer editor的时候，传进来的**part是MultiPageTalendEditor，而不是**JavaInformationProvider保存的TalendJavaEditor，所以该**listener永远不可能被反注册掉，GC也就不可能回收**TalendJavaEditor。对于该软件系统来所，这是一个致命的内存溢出问题**

**转自：[http://blog.csdn.net/moneyice/article/details/2644503](http://blog.csdn.net/moneyice/article/details/2644503)**