Positioning method jvm memory overflow problem

Positioning method jvm memory overflow problem

Brought to you today positioning method memory overflow problem JVM Experience.
Ado direct start:

A, Java heap overflow

Test code is as follows:

import java.util.*;
public class A {
    public static void main(String[] args) {
        List<String> strList = new ArrayList<>();
        while(true) {
            strList.add("");
        }
    }
}

Operation procedure is as follows:

Here we can see the memory continues to rise, while Java will burst at some point OOM exception.
as follows:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3210)
at java.util.Arrays.copyOf(Arrays.java:3181)
at java.util.ArrayList.grow(ArrayList.java:265)
at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:239)
at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:231)
at java.util.ArrayList.add(ArrayList.java:462)
at A.A.main(A.java:9)

Now the program is very simple, but if the explosion in the large-scale projects in a heap overflow in trouble.
It is difficult to locate through the code.
So we need tools for their analysis.


Second, the snapshots of heap memory

We were first on the Eclipse run configuration.


Parameters are shown
-XX:+HeapDumpOnOutOfMemoryError -Xms20m -Xmx20m
results are as follows:

Then we find projects address, we will find that there has been a hprof file in this catalog Project


At this point we took a snapshot of heap memory preserved.


Third, analysis

We need here is a tool called MAT analysis tools provided by Eclipse.

Here is the link we choose Tsinghua mirror

https://mirrors.tuna.tsinghua.edu.cn/eclipse/mat/1.9.0/rcp/MemoryAnalyzer-1.9.0.20190605-win32.win32.x86_64.zip

After extracting run

here run will be very slow, need to wait for a long time. Open as shown in FIG.

Use this tool to dump things down before we go into

Opening the dominator_tree, we will find the problem stored in main thread,


There are two parameters, one is Shallow Heap, the other is Retained Heap

shallowHeap Retained Heap
Size in memory of the object itself The sum of the size of the object itself takes the size of the object reference +

Conventional understanding is that if we do it GC, it will relieve much RAM.

Of which the object is the Object of the String class we created earlier

So far we have completed the localization of memory errors.

Guess you like

Origin www.cnblogs.com/godoforange/p/11544004.html