difference between memory leak and memory overflow

memory leak

Memory leak (memory leak): It means that the program cannot release the memory space that has been applied after applying for memory. The harm of a memory leak can be ignored, but the accumulation of memory leaks has serious consequences. No matter how much memory, it will be occupied sooner or later. 
Generally speaking, the memory leak refers to the leak of the heap memory. The heap memory refers to the program allocated from the heap, and the size is arbitrary (the size of the memory block can be determined during the program runtime). After the use is completed, the memory must be released. The application generally uses malloc, realoc, new and other functions to allocate a memory block from the heap. After the use is completed, the program must be responsible for the corresponding release. In Java, manual release is not required due to the garbage collection mechanism. 
If the memory cannot be released, the memory cannot be used again, and we say that the memory leaks.

memory overflow

Out of memory: The program requires more memory than the system can allocate.

Classified by the way they occur, memory leaks can be divided into 4 categories: 
1. Frequent memory leaks. The code that has a memory leak will be executed multiple times, and each time it is executed, it will cause a memory leak. 
2. Occasional memory leaks. Code that leaks memory only occurs under certain circumstances or operating procedures. Frequent and occasional are relative. For certain circumstances, the occasional may become the regular. So the test environment and test method are crucial to detect memory leaks. 
3. One-time memory leak. The code that leaks the memory will only be executed once, or due to an algorithmic flaw, there will always be one and only one piece of memory that leaks. For example, memory is allocated in the constructor of the class, but not released in the destructor, so the memory leak will only happen once. 
4. Implicit memory leaks. The program keeps allocating memory while it is running, but does not release the memory until it finishes. Strictly speaking, there is no memory leak here, because the final 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 also lead to eventually exhausting all the memory of the system. Therefore, we call this type of memory leak an implicit memory leak.

From the point of view of the user using the program, the memory leak itself will not cause any harm. As a general user, the existence of the memory leak will not be felt at all. What is really harmful is the accumulation of memory leaks, which can eventually consume all the memory in 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 occasional memory leaks

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326313029&siteId=291194637
Recommended