What is the difference between a memory overflow and a memory leak? How to solve memory overflow? Memory overflow means that when a program applies for memory, it does not have enough memory space for it to use, resulting in out of memory; For example, if you apply for an integer, but save the number that can only be saved by long, that is, memory overflow. Memory leak means that the program can’t release the applied memory space after applying for memory. The harm of a memory leak can be ignored, but the consequences of memory leak accumulation are very serious. No matter how much memory, it will be used up sooner or later.

The difference between memory overflow and memory leak

Memory overflow out of memory refers to the phenomenon that the program does not have enough memory space to use when it applies for memory; For example, if you apply for an integer, but save the number that can only be saved by long, that is, memory overflow.

Memory leak means that the program can’t release the applied memory space after applying for memory. The harm of a memory leak can be ignored, but the consequences of memory leak accumulation are very serious. No matter how much memory, it will be used up sooner or later.

Memory leak will eventually lead to out of memory!

Memory overflow is that the memory you want to allocate exceeds what the system can give you, and the system can’t meet the demand, so there is an overflow.

Memory leak refers to that you apply to the system for allocating memory for use (New), but do not return it after use (delete). As a result, the memory you applied for can no longer be accessed by yourself (maybe you lost its address), and the system can not allocate it to the required program again. A plate can only hold 4 fruits in all kinds of ways, but you can’t eat them when you put 5 on it. This is overflow! For example, when the stack is full, it will generate space overflow, which is called overflow. When the stack is empty, it will also generate space overflow, which is called underflow. If the allocated memory is not enough to put down the data item sequence, it is called memory overflow.

According to the way of occurrence, memory leaks can be divided into four categories

1. Frequent memory leaks. The code with memory leak will be executed many times, and each execution will lead to a memory leak.

2. Accidental memory leak. The code with memory leak can only happen in some specific environment or operation process. Frequent and incidental are relative. For a particular environment, occasional may become frequent. So test environment and test method are very important to detect memory leak.

3. One time memory leak. Memory leak code will only be executed once, or due to the defect of algorithm, there will always be only one memory leak. For example, memory is allocated in the constructor of a class, but it is not released in the destructor, so a memory leak occurs only once.

4. Implicit memory leak. The program allocates memory continuously in the process of running, but does not release memory until the end. Strictly speaking, there is no memory leak here, because eventually the program releases all the requested memory. But for a server program, it needs to run for several days, weeks or even months. If the memory is not released in time, it may eventually run out of all the memory of the system. Therefore, we call this kind of memory leak implicit memory leak.

From the perspective of users using programs, memory leak itself will not produce any harm. As a general user, there is no memory leak at all. The real danger is the accumulation of memory leaks, which will eventually consume all the memory of the system. From this point of view, a one-time memory leak is not harmful because it does not accumulate, while an implicit memory leak is very harmful because it is more difficult to detect than frequent and accidental memory leaks.

Causes of memory overflow and Solutions

There are many reasons for memory overflow. The following are common ones

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

2. There are references to objects in the collection class, which are not cleared after use, so that the JVM cannot recycle;

3. There are dead loops in the code or too many duplicate object entities generated by loops;

4. Bug in the third party software used;

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

Solution to memory overflow:

First, modify the JVM startup parameters to increase the memory directly- XMS, – Xmx parameters must not be added.)

Second, 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 the possible location of memory overflow.

Focus on the following:

1. Check whether there is a query to get all the data at one time in the database query. Generally speaking, if one takes 100000 records to memory at a time, it may cause memory overflow. This problem is relatively hidden. Before going online, there is less data in the database, so it is not easy to have problems. After going online, there is more data in the database, and a query may cause memory overflow. Therefore, for the database query, try to use pagination.

2. Check the code for dead loops or recursive calls.

3. Check whether there is a large loop to generate new object entities repeatedly.

4. Check whether there is a query to get all the data at one time in the database query. Generally speaking, if one takes 100000 records to memory at a time, it may cause memory overflow. This problem is relatively hidden. Before going online, there is less data in the database, so it is not easy to have problems. After going online, there is more data in the database, and a query may cause memory overflow. Therefore, for the database query, try to use pagination.

5. Check whether the list, map and other collection objects are not cleared after use. List, map and other collection objects will always have references to objects, so that these objects cannot be recycled by GC.

The fourth step is to use the memory view tool to dynamically view the computer memory usage.

Tagged:

Leave a Reply

Your email address will not be published. Required fields are marked *