We often talk about memory overflow and memory leak in actual programming, especially for C/C++ programs (the following code examples are all C/C++), because we deal directly with memory at this time. However, many times we cannot fully understand these two concepts, and sometimes even confuse the two. In fact, we can also understand the general meaning of memory overflow and memory leak from the naming. Let me give you an example that may not be appropriate. It is like drawing water from a water tank. Originally, this tank can only hold 5 buckets of water. After the 5th bucket is filled, you insist on adding the 6th bucket. The water in the tank naturally overflows. This is called "memory overflow". After the tank is filled with water, no one uses it. On the second day, it is found that the water in the tank is half gone. On the third day, there is no water left. It turns out that there is a hole in the bottom of the tank that was forgotten to be repaired (why do you need to make a hole in the bottom of the tank? No reason, just for fun, willful). This is like applying for a piece of memory and forgetting to release it, causing a "memory leak". The following will briefly analyze these two concepts. First, let's talk about memory overflow. Simply put, memory overflow means that the memory requested for allocation exceeds what the system can provide. For example, if you apply for a 10-byte memory space and you insist on stuffing 11 bytes of data into it, it will naturally be full and overflow (as shown in code example 1). In fact, array out-of-bounds is also a kind of memory overflow, such as exceeding the array range when writing data (reading array data out of bounds is not considered a memory overflow). After the array is filled out of bounds, if you stuff more into it, it will occupy the stack memory (generally, arrays are declared as local variables, and local variables automatically allocate memory in the stack area). The out-of-bounds part is treated as a local variable to occupy the stack memory, because the stack grows from the bottom of the RAM to the top (storing data), and the other data of the program is from the top to the bottom, so when the stack is stored more and more and accumulates higher and higher, the stack will collide with the data when the program is running, and the two will fill the entire RAM memory. At this time, the stack continues to consume, and the stack grows upward, directly overwriting the variables required for the program to run, and the program will run away. So it seems that memory overflow is also very scary.
Next, let's talk about memory leaks. Generally speaking, memory leaks refer to heap memory leaks (Heap leaks). Memory leaks occur when memory is dynamically requested on the heap and is not released in time after use. By the way, if the memory pointed to by the pointer is released, but the pointer is not immediately set to NULL, a wild pointer will be created (as shown in Code Example 2). A single memory leak may not be detected and may not cause any harm, but the accumulation of memory leaks will cause memory exhaustion, which will have serious consequences. For example, memory leaks occur in loop bodies. Of course, there are other forms of memory leaks, such as memory leaks caused by system resource leaks (Resource leaks), etc. According to the frequency of memory leaks, general memory leaks can be divided into the following four types:
|
<<: 7 challenges facing Apple Watch app developers
>>: FB open-sources React Native, using JS to develop native iOS apps
When you look up at the moon at night, do you fin...
After being delayed for more than a month, Douyin...
Advertising traffic is too expensive? SEM prices ...
In the past two days, a piece of news has attract...
Resource introduction of the ninth phase of the L...
【51CTO.com Quick Translation】Writing tests is not...
In nature, even elephants weighing 5 tons will be...
I have read an article published by Diao Ye befor...
Preface: In the previous article, the author disc...
On June 30, India banned 59 Chinese apps for secu...
Is Japan no longer able to print money? Next year...
The annual Chinese Valentine's Day is approac...
There is a saying in the branding industry that “...
[[151452]] Preface This post is a compilation of ...
Introduction It is worth noting that BAT has also...