Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

Please follow OpenCoder and make friends with me ~😍😊😘

A brief introduction.

Eclipse Memory Analyzer is a fast and feature-rich Java heap Analyzer that helps you find Memory leaks and reduce Memory consumption. Memory Analyzer is used to analyze efficient heap dumps with hundreds of millions of objects, quickly calculate the retention size of objects, see who is preventing the garbage collector from collecting objects, and run reports to automatically extract suspected leaks.

With the MAT tool, you can do the following:

  • Find the largest object, because MAT provides a reasonable cumulative size.
  • Explore the object diagram, including inbound and outbound references
  • Calculates the path from GC Roots to interesting objects
  • Look for memory waste, such as redundant String objects, empty collection objects, etc…

Download address

www.eclipse.org/mat/downloa…

At present, most development tools already use IDEA, so you can download independent MAT, note: independent MAT needs to run in JDK11 or higher environment.

3. Heap Dump

First, a Heap Dump, also known as a Heap Dump, is a snapshot of a Java process at some point in time. It holds a lot of information when the snapshot is triggered: Java object and class information. Typically, a full GC is triggered before writing to the heap dump, so it contains information about the remaining objects.

Memory Analyzer is able to process HPROF binary heap dumps from a variety of platforms, IBM system dumps (older versions require preprocessing), and IBM Portable heap dumps (PHD).

The information you can get in Heap Dump includes:

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

  • 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

Here is a translation for you:

Typical information that can be found in a heap dump (depending on the heap dump type) :

  • All object classes, fields, raw values, and references

  • All Classes

    Class loaders, names, superclasses, static fields

  • Garbage collection root objects GC Roots are defined as objects accessible by the JVM (e.g., local variables and statically like variables)

  • The call stack for the thread stack and local variable snapshot time, along with per-frame information about the local object

How to obtain Dump

The Dump file is in the format of HPROF. The memory analyzer can process heap dumps in the binary format of HPROF

Dump = Dump; Dump = Dump;

  • Non-interactive passive acquisition:

    Parameters can be added to the JVM using OOM, i.e., a HPROF binary Heap Dump file after OutOfMemoryError (no overhead unless OOM actually occurs).

    -XX:+HeapDumpOnOutOfMemoryError
    Copy the code

    This parameter is required for production systems because it is often the only way to further analyze memory leaks.

    By default, the heap dump is generated in the CURRENT directory of the JVM. It can be explicitly redirected using ** -xx :HeapDumpPath=, for example -xx :HeapDumpPath=/disk2/dumps. Note that the dump file can be large, up to gigabytes, so make sure the target file system has enough space.

  • Interactive acquisition

    1. To obtain a Heap Dump, add the following parameters to the VM and press Ctrl+Break:

      -XX:+HeapDumpOnCtrlBreak
      Copy the code
    2. Generate it using the Jmap tool. Enter the following command line:

      Jmap-dump :format=b file=< filename xx. hprof> <pid>Copy the code
    3. Sun JConsole: Start jConsole.exe and invoke operation dumpHeap() on HotSpotDiagnostic MBean

    4. Use the File -> Acquire Heap Dump function of Memory Analyzer Tools

To perform operations on a GUI, select the PID process you want to dump and set a path for storing files.

5. Use MAT

Open the dump file with the MAT tool. After selecting the file, you will be asked to select the type of report. The default is memory leak detection report.

Memory Leak Suspects

After entering MAT, it will show the distribution of suspected memory leaks in memory and list specific classes. If it is your own code, you can basically locate some businesses at a glance. If you are not sure, it doesn’t matter

Analysis of a possible memory leak by Problem Suspect1 on the chart above:

Thread Java.lang.Thread @0xFF626d38 main keeps local variables with total size 7,293,920 (89.25%) bytes.

Persistent references to local variables in the thread’s main reached 7.3m of memory, occupying 89.25% of the heap

The memory is accumulated in one instance of java.lang.Object[] , loaded by ****, Which will be later!

Memory is accumulated in an instance of “java.lang.object []”, loaded by “, occupying 7,292,936 (89.24%) bytes. It’s very clear that this array takes up a lot of memory.

So what exactly is in this array?

In the lower left corner of the figure, you can see the Details:

Java.util.ArrayList is a java.lang.Object[] array. From this we can know exactly what Object occupies too much memory, so MAT analysis of memory is very convenient.

Trace the thread execution stack to find the problem code

Once you find that a thread has created a large number of objects during its execution, you can try to find out what code the thread executed to create the objects by clicking ** “See StackTrace” ** in the image below:

Click in to find the exact number of lines of code where the problem occurred:

Here we post the corresponding source code, actually very simple, as follows:

The code problem is in line 10, where the list adds elements.

Seven Histogram.

We did our analysis in Leak’s Suspects, but we can also open a much-used tool called Histogram

Click in:

Here is an explanation of the parameters for each column:

  • Class Name: Indicates the Name of the Class
  • Objects: The number of Objects of the class, and how many of these Objects are created
  • Shallow Heap: The amount of memory consumed by an object, which contains no references to other objects
  • Retained Heap: is the total of the shallow Heap, that is, the amount of the Retained Heap that can be retrieved from memory after the Retained Heap is GC

You can see that the Object[] array takes up 7.4MB.

Right-click on one of these items and select List Objects -> With Incoming Refs to list instances of this class:

And you can see that this is also an intuitive way to figure out what our big object really is.

Ok, I believe that through the explanation of this article, everyone has a certain understanding and reference for the use of MAT tool. In the future, memory leaks, memory lag and large object data analysis can be directly determined accurately through MAT, so as to better optimize our project.

Please follow OpenCoder and make friends with me ~😍😊😘