Java heap memory overflow display and optimization ideas

Java heap memory overflow display and optimization ideas

1. JAVA heap overflow
This article only focuses on java heap memory overflow, not other memory overflows: the
previous article "Learning Notes for the Java Virtual Machine" has said that the Java heap is used to store object instances. So to demonstrate the OOM effect, we will create new object instances without limit, and ensure that there is an accessible path between GC Roots and objects to avoid garbage collection mechanisms to clear these objects, when the number of instances touches the heap When the maximum capacity value is reached, an OutOfMemoryError will occur;

2. Simulate heap memory overflow test

Since everyone’s computer configuration is very high now, in order to see the abnormal Java heap memory overflow as soon as possible, we can reset the values ​​of -Xmx and -Xms to 20M; the following shows the memory increase effect through jconsole, so we did not set these two Values;

Test code

package com.demo.spring.test.jvm;

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

/**
 * @Description: java堆内存溢出测试
 * @Author: yangshilei
 * 为了方便测试:需要将idea或者eclipse的-Xms和-Xmx参数均设置为20M;
 * 1.-Xms:堆的最小值
 * 2.-Xmx:堆的最大值
 * 3.-Xms的值等于-Xmx表示避免堆内存自动扩展
 */
public class HeapOutOfMemory {
    
    

    static class OOMObject{
    
    
        public OOMObject(){
    
    
            System.out.println("hello world");
        }
    }

    public static void main(String[] args) {
    
    

        List<OOMObject> list = new ArrayList<>();

        // 在无限循环中不停的创建对象实例
        while (true){
    
    
            list.add(new OOMObject());
        }
    }
}

We can use jconsole to look at the memory changes when the program is executed: the heap memory is also recycled, but the recycling is not clean every time, and it gradually increases, resulting in an increase in the heap memory.
Insert picture description here
We can also see the current memory and maximum memory usage in the VM summary:
Insert picture description here
when memory overflow is reached: jconsole displays the connection exception error result, as follows:
Insert picture description here

十一月 24, 2020 10:29:28 上午 ClientCommunicatorAdmin Checker-run
警告: Failed to check connection: java.net.SocketException: Connection reset
十一月 24, 2020 10:29:28 上午 ClientCommunicatorAdmin Checker-run
警告: stopping

The abnormal result returned by the code program at this time:
Insert picture description here

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2245)
    at java.util.Arrays.copyOf(Arrays.java:2219)
    at java.util.ArrayList.grow(ArrayList.java:213)
    at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:187)
    at java.util.ArrayList.add(ArrayList.java:411)
    at com.demo.test.HeapOOM.main(HeapOOM.java:12)

When we see OutOfMemoryError, we still don't know what the memory overflow is, follow the further prompt "Java heap space", it can be judged as java heap memory overflow!

Third, the optimization method of heap memory overflow
1. Check the program code :
First, check whether the program has an infinite loop to create an instance code. If this is not solved, no amount of memory will be enough; secondly, check whether there is something in the code. The life cycle of the object is too long, the holding state time is too long, the storage structure design is unreasonable, etc., try to reduce the memory consumption during the running of the program.

2. Increase the Java virtual machine -Xms and -Xmx values :
if the machine's memory has extra space, we will increase these two values ​​appropriately;
generally speaking: in order to avoid the java garbage collection mechanism after cleaning up the heap area There is no need to re-separate and calculate the size of the heap area and waste resources. Usually the two values ​​are set to the same size.
-Xms: indicates the initial memory allocation size of the heap area of ​​the java virtual machine, usually 1/64 of the available memory of the operating system
-Xmx: indicates the maximum upper limit of the heap area memory of the java virtual machine that can be allocated, usually the operating system 1/4 of the available memory.

Guess you like

Origin blog.csdn.net/qq_37488998/article/details/109902796