MAT simple analysis of memory overflow problem

Simulate a memory overflow situation

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
 * 向List集合中添加100万个字符串,每个字符串由1000个UUID组成。如果程  序能够正常执行,最后打印ok。
 */
public class TestJvmOutOfMemory {
    
    
    public static void main(String[] args) {
    
    
        List<Object> list = new ArrayList<>();
        for (int i = 0; i < 10000000; i++) {
    
    
            String str = "";
            for (int j = 0; j < 1000; j++) {
    
    
                str += UUID.randomUUID().toString();
            }
            list.add(str);
        }
        System.out.println("ok");
    }
}

insert image description here

-Xms8m -Xmx8m -XX:+HeapDumpOnOutOfMemoryError

insert image description here

Analysis with MAT

insert image description here
insert image description here
It can be seen from the analysis results that 92.16% of the memory is occupied by Object[], so it is suspicious.
insert image description here
insert image description here

An overview of the main functions of the MAT interface

insert image description here
insert image description here
insert image description here
insert image description here

Generate heap dump in real time

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

Histogram

The bar chart shows the usage of each class, such as the number of classes, the space occupied, etc.
insert image description here
insert image description here

grouping

insert image description here

sort

insert image description here

Regular search

insert image description here

View citations

insert image description here
insert image description here

code

import java.util.ArrayList;
import java.util.Random;
/**
 * -Xms30m -Xmx30m -XX:SurvivorRatio=8 -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps
   -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/opt/dump.hprof
 */
public class OOMTest {
    
    
    public static void main(String[] args) throws InterruptedException {
    
    
        ArrayList<Box> list = new ArrayList<>();
        while(true){
    
    
            Thread.sleep(100);
            list.add(new Box(new Random().nextInt(1024 * 1024)));
        }
    }
}

class Box{
    
    
    private byte[] data;

    public Box(int length) {
    
    
        this.data = new byte[length];
    }
}

Comparing two histograms

insert image description here
insert image description here

In and out references

insert image description here
insert image description here
insert image description here
insert image description here

domination tree

insert image description here
insert image description here

Display thread information

insert image description here
insert image description here

Display heap overall information

insert image description here
Display overall heap information, such as some objects that consume the most.

Guess you like

Origin blog.csdn.net/fengsheng5210/article/details/123681123