[Java] What is the difference between a memory leak and a memory overflow?

foreword

1. Memory Leak

It means that after the program applies for memory, it cannot release the applied memory space. A memory leak does not seem to have a big impact, but the consequence of memory leak accumulation is memory overflow.

2. Memory Overflow

It means that when the program applies for memory, there is not enough memory for the applicant to use, or in other words, you are given a storage space for storing int type data, but you store long type data, then the result is that the memory is not enough, and an error OOM will be reported at this time, which is the so-called memory overflow.

3. The relationship between the two

The accumulation of memory leaks will eventually lead to memory overflow. Memory overflow means that the memory space you want exceeds the space actually allocated to you by the system. At this time, the system is equivalent to unable to meet your needs, and it will report a memory overflow error.

A memory leak means that you apply to the system to allocate memory for use (new), but you do not return it (delete) after use. As a result, you can no longer access the memory you applied for (maybe you lost its address), and the system cannot allocate it to the required program again. It is equivalent to renting a locker with a key, and after you lock the locker after storing your things, you lose the key or fail to return the key, then the result is that the locker will not be available for anyone to use, nor can it be recycled by the garbage collector, because no information about it can be found.

Memory overflow: A plate can only hold 4 fruits through various methods, but you put 5 fruits, but it falls on the ground and cannot be eaten. This is overflow. For example, when the stack is full, pushing it into the stack will inevitably cause space overflow, which is called overflow. That is, the allocated memory is not enough to put down the sequence of data items, which is called memory overflow. To put it bluntly, I can't bear so much, so I will report an error~

4. Classification of memory leaks (by way of occurrence)

Frequent memory leaks: The code with memory leaks will be executed multiple times, and each time it is executed, it will cause a memory leak.
Occasional memory leaks: codes with memory leaks only occur in certain specific environments or operating procedures.
Frequent and sporadic are relative. For a specific environment, sporadic may become regular. So the test environment and test method are crucial to detect memory leaks.
One-time memory leak: The code with a memory leak will only be executed once, or due to an algorithmic defect, there will always be one piece of memory leaked. For example, memory is allocated in the constructor of the class, but not released in the destructor, so the memory leak will only occur once.
Implicit memory leak: The program keeps allocating memory during the running process, but does not release the memory until the end. Strictly speaking, there is no memory leak here, because eventually the program releases all the allocated memory. But for a server program that needs to run for days, weeks or even months, not releasing the memory in time may eventually exhaust all the memory of the system. Therefore, we call this type of memory leak an implicit memory leak.

5. Causes and solutions of memory overflow

5.1. Reasons for memory overflow

  1. The amount of data loaded in the memory is too large, such as taking 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 be recycled;
  3. There is an infinite loop in the code or the loop generates too many repeated object entities;
  4. Bugs in the third-party software used;
  5. The startup parameter memory value is set too small.

5.2, the solution of memory overflow

The first step is to modify the JVM startup parameters and directly increase the memory. (-Xms, -Xmx parameters must not forget to add)

The second step is to check the error log to see if there are other abnormalities 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.

Guess you like

Origin blog.csdn.net/u011397981/article/details/131832045
Recommended