JAVA内存分析工具--MemoryAnalyzer使用入门

工具下载

https://www.eclipse.org/mat/downloads.php
在这里插入图片描述

文档使用帮助

直接看help帮助文档。
在这里插入图片描述

Concepts

Heap Dump

这里是引用Heap Dump
A heap dump is a snapshot of the memory of a Java process at a certain point of time.There are different formats for persisting this data, and depending on the format, maycontain different pieces of information; but, in general, the snapshot contains informationabout the Java objects and classes in the heap at the moment the snapshot was triggered.In some cases, a full GC is triggered before the heap dump is written so it containsinformation about the remaining objects.
The Memory Analyzer is able to work with HPROF binary heap dumps,IBM system dumps (old versions require preprocessing), and IBM portable heap dumps (PHD)from a variety of platforms.
Typical information which can be found in heap dumps (depending on the heap dump type):
•All Objects
Class, fields, primitive values and references
•All Classes
Classloader, name, super class, static fields
When MAT parses IBM system dumps, the size of classes includes some of the amountof native memory in the Java process (but outside of the Java heap) which is relatedto those classes such as native memory for bytecode and JIT compiled code for the classmethods. In some cases, this may cause the total size reported on the Overview pane toexceed the maximum Java heap size. This calculation may be disabled using an optionin the MAT configuration.
•Garbage Collection Roots
Objects defined to be reachable by the JVM
•Thread Stacks and Local Variables
The call-stacks of threads at the moment of the snapshot, and per-frame information about local objects
A heap dump does not contain allocation information so it cannotresolve questions like who had created the objects and where theyhave been created.

1.Heap Dump
Heap Dump 是一个 Java 进程在某个时间点上的内存快照。 Heap Dump 是有着多种格式的。 不过总体上
Heap Dump 在触发快照的时候都保存了 java 对象和类的信息。通常在写 Heap Dump 文件前会触发一次 FullGC,
所以 Heap Dump 文件中保存的是 FullGC 后留下的对象信息。
Memory Analyzer 可以用来处理 HPROF 二进制 Heap Dump 文件、 IBM 系统 dump 文件(经过处理后)、
以及来自各个平台上的 IBM portable Heap Dumps (PHD)文件。
一般在 Heap Dump 文件中可以获取到(这仍然取决于 Heap Dump 文件的类型) 如下信息:
 对象信息:类、成员变量、直接量以及引用值;
 类信息: 类加载器、 名称、 超类、 静态成员;
 Garbage Collections Roots: JVM 可达的对象;
 线程栈以及本地变量: 获取快照时的线程栈信息, 以及局部变量的详细信息。
Heap Dump 文件中并不包含内存分配信息, 所以通常无法通过 Heap Dump 文件解决是谁以及在哪里创
建了哪些对象这样的问题。

Reachability

Reachability
Objects in a heap dump have references to other objects. These can either bevia field references (for simple objects), array elements (for object arrays)or via various hidden references. For instance every object contains a reference toits type, and each class contain a reference to the class loader which loadedthe class.
These objects and references form a directed graph. The objects are the nodes,the references are the directed links between the nodes. Thegarbage collection roots are the roots of this graph.
ReachableAn object is reachable from another object if there is apath following the directed links from the source object to the destination object. Unreachable objectIf there is no path from a garbage collection rootto an object then it is unreachable. There is then no way a legitimate Java programcan every get access to this object, so it is safe for the Java virtual machine todiscard this object from the heap. This process of determining unreachable objects anddiscarding them, thus making room for more objects to be allocated as required, iscalled garbage collection.

可达性
堆转储中的对象具有对其他对象的引用。它们可以是bevia字段引用(用于简单对象)、数组元素(用于对象数组)或通过各种隐藏引用。例如,每个对象都包含对其类型的引用,而每个类都包含对装入类的装入器的引用。
这些对象和引用形成一个有向图。对象是节点,引用是节点之间的定向链接。垃圾收集根是这个图的根。
如果有一个apath跟随源对象到目标对象的定向链接,则可以从另一个对象访问ReachableAn对象。如果没有垃圾收集rootto的路径,那么对象就是不可到达的。因此,合法的Java程序不可能每次都访问这个对象,所以Java虚拟机从堆中丢弃这个对象是安全的。这种确定不可达对象并丢弃它们,从而为需要分配的更多对象腾出空间的过程称为垃圾收集。

Shallow vs. Retained Heap

Shallow heapis the memory consumed by one object. An object needs 32 or 64 bits(depending on the OS architecture) per reference, 4 bytes perInteger, 8 bytes per Long, etc. Depending on the heap dump format thesize may be adjusted (e.g. aligned to 8, etc…) to model better thereal consumption of the VM.
Retained setof X is the set of objects which would be removed by GC when X is garbagecollected.
Retained heapof X is the sum of shallow sizes of all objects in the retained setof X, i.e. memory kept alive by X.
Generally speaking, shallow heap of an object is its sizein the heap and retained size of the same object is the amount ofheap memory that will be freed when the object is garbage collected.
The retained set for a leading set of objects, such as allobjects of a particular class or all objects of all classes loaded bya particular class loader or simply a bunch of arbitrary objects, isthe set of objects that is released if all objects of that leadingset become unaccessible. The retained set includes these objects aswell as all other objects only accessible through these objects. Theretained size is the total heap size of all objects contained in theretained set.
在这里插入图片描述
2.Shallow or Retained Heap
Shallow Heap 表示一个对象消费的内存的总量。 对象的每个引用变量会占用 32 或 64bit(取决于操作系统),每个 Integer 需要占用 4byte,每个 Long 需要占用 8byte,诸如此类的其他信息可以自行查询。 Shallow
heap 的值可能是经过了调整的(比如对齐到 8,具体取决于 Heap Dump 文件的格式), 以便更好地模拟虚拟机的真实消费情况。
对象 X 的 Retained Set 指的是一旦 X 被垃圾回收后也会随之被 GC 回收掉的对象的集合。
对象 X 的 retained heap 指的是 X 的 retained set 中所有对象的 shallow heap 之和,或者说是因为对象 X而保持 alive 的内存的大小。
通常来说, shallow heap 就是对象自身在堆内存中的大小, 而同一个对象的 retained heap 指的是该对象被垃圾回收后释放的堆内存的大小。
一组 leading 对象的 retained set(如一个特定类的全部对象、 一个特定类加载器加载的所有类的全部对象、 又或者一串任意的对象)在 leading 对象集合中的对象全部不可达时被释放掉。 Leading 对象集的 retainedset 包括这些对象本身和其他的只能通过这些对象访问到的对象。而 leading 对象集合的 retained size 指的就是 retained set 中的全部对象的堆内存之和。 如下图:

Dominator Tree

Memory Analyzer provides a dominator tree of the object graph.The transformation of the object reference graph into adominator tree allows you to easily identify the biggest chunks ofretained memory and the keep-alive dependencies among objects.Bellow is an informal definition of the terms.
An object x dominatesan object y if every path in the object graph from the start (or theroot) node to y must go through x.
The immediate dominatorx of some object y is the dominator closest to the object y.
A dominator treeis built out of the object graph. In the dominator tree each objectis the immediate dominator of its children, so dependencies betweenthe objects are easily identified.
The dominator tree has the following important properties:
•The objects belonging to the sub-tree of x(i.e. the objects dominated by x) represent the retained set of x.
•If xis the immediate dominator of y, then the immediate dominator of xalso dominates y, and so on.
•The edges in the dominator tree do not directly correspond toobject references from the object graph.
在这里插入图片描述
Memory Analyzer提供了对象图的控制树。将对象引用图转换为adominator树可以方便地识别保留内存的最大块和对象之间的保持活动依赖项。Bellow是这些术语的非正式定义。
如果对象图中从起点(或起点)节点到y的每条路径都必须经过x,则对象x支配对象y。
某个对象y的直接支配者x是最接近对象y的支配者。
从对象图构建一个控制树。在控制树中,每个对象都是其子对象的直接控制对象,因此对象之间的依赖关系很容易识别。
优势树具有以下重要属性:
•属于x的子树的对象(即由x支配的对象)表示x的保留集合。
•如果x是y的直接支配者,那么x的直接支配者也支配y,以此类推。
•优势树中的边并不直接对应对象图中的对象引用。

Garbage Collection Roots

A garbage collection root is an object that is accessible fromoutside the heap. The following reasons make an object a GC root:
System ClassClass loaded by bootstrap/system class loader. Forexample, everything from the rt.jar like java.util.. JNI LocalLocal variable in native code, such as user definedJNI code or JVM internal code.JNI GlobalGlobal variable in native code, such as userdefined JNI code or JVM internal code.Thread BlockObject referred to from a currently active thread block.ThreadA started, but not stopped, thread.Busy MonitorEverything that has called wait()or notify()or that is synchronized. For example, by calling synchronized(Object)or by entering a synchronized method. Static method means class,non-static method means object. Java LocalLocal variable. For example, input parameters orlocally created objects of methods that are still in the stack of athread.Native StackIn or out parameters in native code, such as userdefined JNI code or JVM internal code. This is often the case asmany methods have native parts and the objects handled as methodparameters become GC roots. For example, parameters used forfile/network I/O methods or reflection.FinalizableAn object which is in a queue awaiting its finalizer to be run. UnfinalizedAn object which has a finalize method, but has not been finalized andis not yet on the finalizer queue. UnreachableAn object which is unreachable from any other root, but has beenmarked as a root by MAT to retain objects which otherwise would notbe included in the analysis. Java Stack FrameA Java stack frame, holding local variables. Only generated when thedump is parsed with the preference set to treat Java stack frames as objects. UnknownAn object of unknown root type. Some dumps, such as IBM Portable Heap Dump files,do not have root information. For these dumps the MAT parser marks objects which are haveno inbound references or are unreachable from any other root as roots of this type.This ensures that MAT retains all the objects in the dump.
垃圾收集根
垃圾收集根是一个可以从堆外部访问的对象。以下原因使对象成为GC根:
系统类类由引导/系统类装入器装入。例如,rt.jar中的一切,比如java.util.
。本地代码中的JNI本地变量,例如用户定义的JNI代码或JVM内部代码。本机代码中的JNI GlobalGlobal变量,如用户定义的JNI代码或JVM内部代码。从当前活动的线程块引用的线程块对象。ThreadA启动了线程,但没有停止线程。忙碌监控所有调用wait()或notify()或同步的内容。例如,通过调用synchronized(对象)或输入synchronized方法。静态方法表示类,非静态方法表示对象。Java LocalLocal变量。例如,输入参数或仍然在athread堆栈中本地创建的方法对象。本机StackIn或out参数在本机代码中,如用户定义的JNI代码或JVM内部代码。这种情况经常发生,因为许多方法都有本机部分,而作为方法参数处理的对象成为GC根。例如,用于文件/网络I/O方法或反射的参数。FinalizableAn对象,它在队列中,等待它的终结器运行。具有finalize方法,但尚未完成且未在finalizer队列上的对象。从任何其他根中都不可及的对象,但MAT将其标记为根以保留不包括在分析中的对象。Java堆栈帧保存本地变量的Java堆栈帧。仅当使用首选项集解析转储以将Java堆栈帧视为对象时生成。根类型未知的UnknownAn对象。有些转储,如IBM便携堆转储文件,没有根信息。对于这些转储,MAT解析器将具有入站引用或无法从任何其他根访问的对象标记为这种类型的根。这将确保MAT保留转储中的所有对象。

堆内存溢出的例子

package heap;

import java.util.ArrayList;
import java.util.List;

public class HeapOOM {

    static class OOMObject{
    }

    public static void main(String[] args) {
        List<OOMObject> list = new ArrayList<OOMObject>();
        long num = 0L;
        while (true){
            list.add(new OOMObject());
            num ++;
            if(num % 1000 == 0){
                System.out.println(num);
            }
        }
    }
}

-Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError

配置JVM参数,将堆大小设置小点,方便快速产生内存溢出文件。
在这里插入图片描述

产生文件在对应工程目录下。java_pid6188.hprof。29M大小
导入MAT中。
在这里插入图片描述
在这里插入图片描述
接下来查看显示信息分析溢出原因即可。

Linux下使用MAT

windows如果是32位,内存大小会有限制,只有在64位机器上进行分析,平时服务器一般都比开发的机器内存大,可能产生的dump文件会很大,所以还是有需要在linux环境下打开dump文件。
参考博文:
MAT linux-https://blog.csdn.net/zlfing/article/details/79992194

下载文件时记得选镜像地址,加快下载速度:
在这里插入图片描述

unzip mat.zip

配置MAT
解压之后,需要修改mat的配置文件,打开MemoryAnalyzer.ini文件,修改-Xmx的值,使大于hprof文件的大小,建议是其2倍大小。

-startup
    plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar
    --launcher.library
    plugins/org.eclipse.equinox.launcher.gtk.linux.x86_64_1.1.100.v20110505
    -vmargs
    -Xms16g
    -Xmx16g

执行分析命令
执行如下命令会输出泄漏嫌疑对象的分析结果,这个结果是比较全面的。

红色字体
··

 ./ParseHeapDump.sh ../dump/jvm.hprof org.eclipse.mat.api:suspects

还支持另外两个分析结果:

org.eclipse.mat.api:overview
org.eclipse.mat.api:top_components 
发布了25 篇原创文章 · 获赞 7 · 访问量 920

猜你喜欢

转载自blog.csdn.net/m0_46485771/article/details/105252385