Interviewer: How to troubleshoot memory leaks and memory overflows?

Insert picture description here

Preface

Let's first understand the composition of the Java heap. For most applications, the Java Heap is the largest piece of memory managed by the Java virtual machine lock. The Java heap is a memory area shared by all threads and is created when the virtual machine starts. The only purpose of this memory area is to store object instances. Almost all object instances allocate memory here

The structure of the heap is as follows
Insert picture description here
. The specific division ratio of the new generation and the old generation is as follows
Insert picture description here
. The main function of the generation is to manage memory more efficiently.

Memory leak and memory overflow are two different concepts

Memory leak: The object is no longer used, but it still occupies memory space and has not been released.
Memory overflow: The heap space is not enough, usually manifested as OutOfMemoryError, memory leak usually leads to memory overflow

Use Java VisualVM to remotely analyze the heap

We can analyze the usage of the heap through the jvisualvm command that comes with jdk

Let's write a program to demonstrate the scene of increasing memory

public class OomDemo {
    
    

    private static final int NUM = 1024;

    public static void main(String[] args) throws InterruptedException {
    
    
        List<byte[]> list = Lists.newArrayList();
        for (int i = 0; i < NUM; i++) {
    
    
            TimeUnit.SECONDS.sleep(1);
            list.add(new byte[NUM * NUM]);
        }
    }
}

Insert picture description here

Execute jvisualvm in the command line to pop up a graphical interface. We can connect to the program on the machine, connect to a remote machine, and analyze and generate snapshot files.

You can clearly see that the heap space is rising. Use a sampler to analyze the source of the rising memory.
Insert picture description here
Good guy, the byte array actually takes up so much memory

If you still can’t see where there is a problem with the program at this time, click on the heap Dump button to monitor the Tab, a heap snapshot will be generated, and then analyze the dump file.

Insert picture description here
There are few byte array instances, but they occupy a lot of memory. Look at the specific references and
Insert picture description here
you can see it in the ArrayList.

Finally, I recommend a plug-in Visual GC, which can clearly see the usage of the heap and collect garbage information.
Click on the tool to select the plug-in
Insert picture description here

Insert picture description here
Of course, you can generate a heapdump file through the jmap command, and then use other tools to analyze

Java virtual machine performance monitoring and troubleshooting tools

Java provides a lot of practical commands for us to analyze the state of the Java virtual machine, such as the following commands

jps: List the running virtual machine processes and display the main class of virtual machine execution

cis@mt002:~$ jps
70208 KmpService
183525 LinkAnalysisServer
25160 MipSerachServer

jmap (Memory Map for Java): Command is used to generate heap dump snapshots (generally called heapdump or dump files)

Configure the following parameters in the startup command to generate a snapshot at OOM, which is convenient for our analysis

-XX:+HeapDumpOnOutOfMemoryError

jstack (Stack Trace for Java): Java stack trace tool to generate a thread snapshot of the virtual machine at the current moment

Can be used to analyze thread deadlock, infinite loop, thread waiting for a long time

Reference blog

[1] https://blog.csdn.net/qq_35190492/article/details/105376377
several reasons for crude overflow
[2] https://blog.csdn.net/cp_panda_5/article/details/79613870

Guess you like

Origin blog.csdn.net/zzti_erlie/article/details/114746000