[JVM] Memory overflow and memory leak

Memory leak : refers to the fact that the program cannot release the allocated memory space after applying for memory. A memory leak does not seem to have a big impact, but the consequence of accumulation of memory leaks is memory overflow.

Memory overflow out of memory : When a program applies for memory, there is not enough memory for the applicant to use.

Classification of memory leaks (classified by how they occur)

  1. Frequent memory leaks . Code with memory leaks will be executed multiple times, causing a memory leak every time it is executed.
  2. Sporadic memory leaks . Code that causes memory leaks will only occur under certain circumstances or operations. Frequent and sporadic are relative. For certain circumstances, what is occasional may become common. So the testing environment and testing methods are crucial to detecting memory leaks.
  3. One-time memory leak . The code that causes a memory leak will only be executed once, or due to algorithmic flaws, there will always be only one and only one block of memory leaked. For example, if memory is allocated in the constructor of a class, but the memory is not released in the constructor, the memory leak will only occur once.
  4. Implicit memory leak . The program continuously allocates memory while it is running, but does not release the memory until the end. Strictly speaking, there is no memory leak here, because the program eventually releases all the requested memory. But for a server program that needs to run for days, weeks or even months, failure to release memory in time may also lead to the eventual exhaustion of all the system's memory. Therefore, we call this type of memory leak an implicit memory leak.

Causes and solutions to memory overflow

Cause of memory overflow

        1. The amount of data loaded in the memory is too large, such as fetching too much data from the database at one time;

        2. There is a reference to the object in the collection class, which is not cleared after use, making it impossible for the JVM to recycle;

        3. There is an infinite loop in the code or the loop generates too many duplicate object entities;

        4. BUG in the third-party software used;

        5. The startup parameter memory value is set too small.

Memory overflow solution

        The first step is to modify the JVM startup parameters and directly increase the memory. (Don’t forget to add the -Xms and -Xmx parameters.)

        The second step is to check the error log to see if there are other exceptions or errors before the "OutOfMemory" error.

        The third step is to walk through and analyze the code to find out where memory overflow may occur.

        Focus on the following points:

        1. Check whether there is a query to obtain all the data at one time in the database query. Generally speaking, if 100,000 records are fetched into the memory at a time, it may cause a memory overflow. This problem is relatively hidden. Before going online, there was less data in the database and it was less likely to cause problems. After going online, there was more data in the database, and a single query may cause memory overflow. Therefore, try to use paging for database queries, especially for large systems.

        2. Check whether there are infinite loops or recursive calls in the code.

        3. Check whether there is a large loop that repeatedly generates new object entities.

        4. Check whether collection objects such as List and Map are not cleared after use. Collection objects such as List and Map will always have references to the objects, making these objects unable to be recycled by GC.

        Step 4 : Use the memory viewing tool to dynamically view memory usage

Guess you like

Origin blog.csdn.net/xudahai513/article/details/125826734