Home > Common Problem > What is the difference between memory leak and memory overflow?

What is the difference between memory leak and memory overflow?

青灯夜游
Release: 2023-02-08 18:33:03
Original
40174 people have browsed it

Difference: Memory overflow means that when the program applies for memory, there is not enough memory space for it to use, and the system can no longer allocate the space you need; memory leak means that after the program applies for memory, it cannot Release the applied memory space. The harm of one memory leak can be ignored, but if the number of memory leaks is too many, it will lead to memory overflow.

What is the difference between memory leak and memory overflow?

The operating environment of this article: Windows 7 system, Dell G3 computer.

Memory overflow (out of memory) means that when the program applies for memory, there is not enough memory space for it to use, and out of memory occurs; for example, an integer is applied for, but the It stores the number that can only be stored by long, which is a memory overflow.

Memory leak (memory leak) means that after the program applies for memory, it cannot release the applied memory space. The harm of a memory leak can be ignored, but the consequences of accumulation of memory leaks are serious. No matter how much memory you have, it will be occupied sooner or later.

Memory leak will eventually lead to out of memory!

Memory overflow means that the memory you request to allocate exceeds what the system can give you, and the system cannot meet the demand, so an overflow occurs.

Memory leak means that you apply to the system to allocate memory for use (new), but do not return it (delete) after using it. As a result, you also lose the memory you applied for. It can no longer be accessed (perhaps you lost its address), and the system cannot allocate it to the required program again. A plate can only hold 4 fruits by all means. You put 5 fruits on it, but they fell to the ground and couldn't be eaten. This is overflow! For example, if a stack is pushed when the stack is full, it will inevitably cause a space overflow, which is called an overflow. If the stack is empty, a space overflow will occur if the stack is empty, which is called an underflow. That is, the allocated memory is not enough to put down the sequence of data items, which is called a memory overflow.

Classified by the way they occur, memory leaks can be divided into 4 categories:

1. Frequent memory leaks. Code with memory leaks will be executed multiple times, causing a memory leak every time it is executed.

2. Occasional 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 destructor, 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.

From the perspective of users using the program, memory leaks themselves will not cause any harm. As ordinary users, they will not feel the existence of memory leaks at all. What's really harmful is the accumulation of memory leaks, which will eventually consume all the system's memory. From this perspective, one-time memory leaks are not harmful because they do not accumulate, while implicit memory leaks are very harmful because they are more difficult to detect than recurrent and sporadic memory leaks.

Related free video tutorial recommendations: "Programming Video"

The above is the detailed content of What is the difference between memory leak and memory overflow?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template