Java jvm memory overflow analysis

1. How to analyze jvm memory overflow?

We often use visualVm to monitor the JVM's memory, CPU, and thread usage. We can usually judge whether the memory exists and is not released based on the continuous growth of the memory. But we can't keep an eye on it all the time. This involves jvm heap memory configuration. Heap memory parameter configuration and tuning will be written in other chapters.

If there is really a memory overflow, we need to configure JVm memory overflow when it occurs online. It is recommended that this parameter XX:+HeapDumpOnOutOfMemoryError must be configured online, otherwise it will be difficult to analyze online problems.

-Xms100m -Xmx100m -XX:+HeapDumpOnOutOfMemoryError 

 2. Write our own code

Next we simulate the memory overflow code 

@Getter
@Setter
@ToString
public class HeapBean {
    private int userId;
    private String name;
    private String phone;
}
package com.es.Controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class HeapController {

    private List<HeapBean> userlist = new ArrayList<>();
    private Map hashMap=new HashMap<>();

    //堆区内存溢出
    @GetMapping("/heapOom")
    public void heapOverTest() {
        int i = 0;
        while (true) {
            //heaplist.add(new heapBean());
            hashMap.put(i, new HeapBean());
            i++;
        }
    }
}

Start the code locally and then request the actual address.

After running for a certain time, the hprof file will be generated.

java.lang.OutOfMemoryError: GC overhead limit exceeded
Dumping heap to java_pid16868.hprof ...
Heap dump file created [171733163 bytes in 0.854 secs]
Exception in thread "File Watcher" java.lang.OutOfMemoryError: GC overhead limit exceeded
	at java.lang.String.toLowerCase(String.java:2647)
	at java.io.WinNTFileSystem.hashCode(WinNTFileSystem.java:640)
	at java.io.File.hashCode(File.java:2132)
	at org.springframework.boot.devtools.filewatch.FileSnapshot.hashCode(FileSnapshot.java:72)
	at java.util.HashMap.hash(HashMap.java:338)
	at java.util.HashMap.put(HashMap.java:611)
	at java.util.HashSet.add(HashSet.java:219)
	at org.springframework.boot.devtools.filewatch.DirectorySnapshot.collectFiles(DirectorySnapshot.java:70)
	at org.springframework.boot.devtools.filewatch.DirectorySnapshot.collectFiles(DirectorySnapshot.java:67)

Find this file through everything.

 

3. Need to use the eclipse Memory Analyzer tool

Eclipse Memory Analyzer (MAT for short) is a feature-rich and simple-to-operate JVM Heap Dump analysis tool that can be used to assist in discovering memory leaks and reducing memory usage.

click to download

 Click "MemoryAnalyzer.exe" to start the memory analysis tool.

 Open the java_pid16868.hprof file

Click Finish according to the red option to confirm, and you will see the largest 40.5MB occupied.

Move down to see the specific occupied classes

Click details to see our own class.

 Continue down to see the total number of objects and the heap memory occupied.

4. Summary 

 Summarize the memory overflow caused by hashMap in HeapControler.

Guess you like

Origin blog.csdn.net/zy08403/article/details/132759912