文档章节

Memory Analyzer tool(MAT)分析内存泄漏

Oscarfff
 Oscarfff
发布于 2016/07/04 13:39
字数 1328
阅读 97
收藏 0

实例分析参考 http://www.blogjava.net/rosen/archive/2010/06/13/323522.html

http://eclipsesource.com/blogs/2013/01/21/10-tips-for-using-the-eclipse-memory-analyzer/

一、首先理解几个概念

Shallow Heap Size

指对象自身所占用的内存大小,不包含其引用的对象所占的内存大小。

1、数组类型

数组元素对象所占内存的大小总和。

2、非数组类型

对象与它所有的成员变量大小的总和。当然这里面还会包括一些java语言特性的数据存储单元。

Retained Heap Size

前对象大小+当前对象可直接或间接引用到的对象的大小总和。

(间接引用的含义:A->B->C, C就是间接引用)
换句话说,Retained Size就是当前对象被GC后,从Heap上总共能释放掉的内存。
不过,释放的时候还要排除被GC Roots直接或间接引用的对象。他们暂时不会被被当做Garbage。

GC Roots直接引用了A和B两个对象。

A对象的Retained Size=A对象的Shallow Size
B对象的Retained Size=B对象的Shallow Size + C对象的Shallow Size

这里不包括D对象,因为D对象被GC Roots直接引用。
如果GC Roots不引用D对象呢?

 

GC Roots

The so-called GC (Garbage Collector) roots are objects special for garbage collector. Garbage collector collects those objects that are not GC roots and are not accessible by references from GC roots.

There are several kinds of GC roots. One object can belong to more than one kind of root. The root kinds are:

  • Class - class loaded by system class loader. Such classes can never be unloaded. They can hold objects via static fields. Please note that classes loaded by custom class loaders are not roots, unless corresponding instances ofjava.lang.Class happen to be roots of other kind(s).
  • Thread - live thread
  • Stack Local - local variable or parameter of Java method
  • JNI Local - local variable or parameter of JNI method
  • JNI Global - global JNI reference
  • Monitor Used - objects used as a monitor for synchronization
  • Held by JVM - objects held from garbage collection by JVM for its purposes. Actually the list of such objects depends on JVM implementation. Possible known cases are: the system class loader, a few important exception classes which the JVM knows about, a few pre-allocated objects for exception handling, and custom class loaders when they are in the process of loading classes.Unfortunately, JVM provides absolutely no additional detail for such objects. Thus it is up to the analyst to decide to which case a certain "Held by JVM" belongs.

二、官方关于如何使用MAT的说明

http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.mat.ui.help%2Fgettingstarted%2Fbasictutorial.html

Memory Analyzer > Getting Started

Basic Tutorial

This tutorial provides a "jumping-off place" to get familiar with Memory Analyzer.

Step 1 - Getting a Heap Dump

The Memory Analyzer works with heap dumps . Such a heap dump contains information about all Java objects alive at a given point in time. All current Java Virtual Machines can write heap dumps, but the exact steps depend on vendor, version and operation system. Find out more in the section Acquiring Heap Dumps .

Open  a sample heap dump if you view this page inside the Eclipse help center.

For the purpose of this tutorial, we use Java 6 and JConsole on Windows. Start your application with Java 6, then start <jre6>/bin/jconsole.exe and select the running application (in this case Eclipse):

JConsole dialog to open a connection to a Virtual Machine.

Then, select the operation dumpHeap from the com.sun.management.HotSpotDiagnostic MBean. The first parameter p0 is the full path to the heap dump file. Make sure you give it the file extension .hprof. The second parameter p1 should be left at true as we are only interested in live objects.

Select the dumpHeap method of the HotspotDiagnostics mbean.

Step 2 - The Overview

Open the heap dump via File >  Open Heap Dump... to see the overview page.

Memory Analyzer's overview page for a heap dump

On the right, you'll find the size of the dump and the number of classes, objects and class loaders.

Right below, the pie chart gives an impression on the biggest objects in the dump. Move your mouse over a slice to see the details of the objects in the object inspector on the left. Click on any slice to drill down and follow for example the outgoing references.

Step 3 - The Histogram

Select the histogram from the tool bar to list the number of instances per class, the shallow size and the retained size .

Histogram

The Memory Analyzer displays by default the retained size of individual objects. However, the retained size of a set of objects - in this case all instances of a particular class - needs to be calculated.

To approximate the retained sizes for all rows, pick Calculate retained size icon from the tool bar. Alternatively, select a couple rows and use the context menu.

Select calculate retained sizes from the tool bar

Using the context menu , you can drill-down into the set of objects which the selected row represents. For example, you can list the objects with outgoing or incoming references. Or group the objects by the value of an attribute. Or group the collections by their size. Or or or...

One thing that makes the Memory Analyzer so powerful is the fact that one can run any action on any set of objects. Just drill down and slice your objects the way you need them.

Drill down via the context menu

Another important feature is the facility to group any histogram by class loader, packages or superclass .

Group the histogram by class loader or package via the tool bar

Any decent application loads different components by different class loaders. The Memory Analyzer attaches a meaningful label to the class loader - in the case of OSGi bundles it is the bundle id. Therefore it becomes a lot easier to divide the heap dump into smaller parts.

More: Analyze Class Loader

Histogram grouped by class loader

Grouping the histogram by packages allows to drill-down along the Java package hierarchy.

Histogram grouped by packages

Grouping the histogram by superclass provides an easy way to find for example all the subclasses of java.util.AbstractMap, etc...

Histogram grouped by superclass

Step 4 - The Dominator Tree

The dominator tree displays the biggest objects in the heap dump. The next level of the tree lists those objects that would be garbage collected if all incoming references to the parent node were removed.

The dominator tree is a powerful tool to investigate which objects keep which other objects alive. Again, the tree can be grouped by class loader (e.g. components) and packages to ease the analysis.

Dominator Tree

Step 5 - Path to GC Roots

Garbage Collections Roots (GC roots) are objects that are kept alive by the Virtual Machines itself. These include for example the thread objects of the threads currently running, objects currently on the call stack and classes loaded by the system class loader.

The (reverse) reference chain from an object to a GC root - the so called path to GC roots - explains why the object cannot be garbage collected. The path helps solving the classical memory leak in Java: those leaks exist because an object is still referenced even though the program logic will not access the object anymore.

Select path to GC roots from the context menu

Initially, the GC root reached by the shortest path is selected.

Path to GC roots

Step 6 - The Leak Report

The Memory Analyzer can inspect the heap dump for leak suspects, e.g. objects or set of objects which are suspiciously big.

Run the leak report

Learn more in this blog posting: Automated Heap Dump Analysis: Finding Memory Leaks with One Click .

Related concepts

Dominator Tree

Garbage Collection Roots

 

本文转载自:http://www.yourkit.com/docs/90/help/gc_roots.jsp

Oscarfff
粉丝 73
博文 816
码字总数 97116
作品 0
崇明
后端工程师
私信 提问
[Android]App 内存泄漏检查工具MAT

Android App发生内存泄漏,常见的有Bitmap 使用后沒有recycle(),Drawable 使用后沒有setCallback(null)等。 Eclipse 有个插件工具MAT(Memory Analyzer Tool)可以帮助定位内存泄漏的对象。 ...

清水湾2012
2013/08/01
475
1
Eclipse中安装MemoryAnalyzer插件及使用

Eclipse中安装MemoryAnalyzer插件 一、简介   Eclipse作为JAVA非常好用的一款IDE,其自带的可扩展插件非常有利于JAVA程序员的工作效率提升。   MemoryAnalyzerTool(也叫MAT)是一款JAVA虚...

gdy
2017/08/02
0
0
DDMS的使用、内存溢出的调试和模拟器的启动命令参数

http://blog.csdn.net/qeqeqe236/article/details/7338608 1. 如果是eclipse自动生成的.hprof文件,可以使用MAT插件直接打开(可能是比较新的ADT才支持); 2. 如果eclipse自动生成的.hprof文...

塔塔米
2014/02/28
99
0
性能优化——内存泄漏(2)工具分析篇

内存泄漏系列文章: 性能优化——内存泄漏(1)入门篇 性能优化——内存泄漏(2)工具分析篇 性能优化——内存泄漏(3)代码分析篇 一、简述 在上一篇《性能优化——内存泄漏(1)入门篇》中...

CSDN_LQR
2017/06/30
0
0
Android内存优化(五)详解内存分析工具MAT

相关文章 Android性能优化系列 Java虚拟机系列 前言 在这个系列的前四篇文章中,我分别介绍了DVM、ART、内存泄漏和内存检测工具的相关知识点,这一篇我们通过一个小例子,来学习如何使用内存...

刘望舒
2017/08/24
0
0

没有更多内容

加载失败,请刷新页面

加载更多

一起来学Java8(四)——复合Lambda

在一起来学Java8(二)——Lambda表达式中我们学习了Lambda表达式的基本用法,现在来了解下复合Lambda。 Lambda表达式的的书写离不开函数式接口,复合Lambda的意思是在使用Lambda表达式实现函...

猿敲月下码
31分钟前
9
0
debian10使用putty配置交换机console口

前言:Linux的推广普及,需要配合解决实际应用方能有成效! 最近强迫自己用linux进行实际工作,过程很痛苦,还好通过网络一一解决,感谢各位无私网友博客的帮助! 系统:debian10 桌面:xfc...

W_Lu
今天
10
0
aelf Enterprise 0.8.0 beta有奖公测,“Bug奖金计划”重磅开启

2019年9月30日,aelf Enterprise 0.8.0 beta版正式发布。aelf Enterprise 0.8.0 beta是一个完备的区块链系统, 包含完备的区块链系统、开发套件、开发文档、以及配套的基础应用和基础服务。 ...

AELF开发者社区
今天
10
0
oracle 初始化数据库脚本

create user lpf identified by 123456; create tablespace lpf_ts_cms datafile '/opt/app/oracle/product/11.2.0/lpf.dbf' size 200M; alter user lpf default tablespace lpf_ts_cms; sel......

internetafei
今天
8
0
深入了解Redis底层数据结构

说明 说到Redis的数据结构,我们大概会很快想到Redis的5种常见数据结构:字符串(String)、列表(List)、散列(Hash)、集合(Set)、有序集合(Sorted Set),以及他们的特点和运用场景。不过它们是...

TurboSanil
今天
7
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部