Analysis of memory overflow and memory leak

Analysis of memory overflow and memory leak

[[130745]]

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.

  1. void arr_test()
  2. {
  3.     int arr[ 10 ];
  4. arr[ 10 ] = 10 ;
  5. }

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:
Frequent memory leaks;
Occasional memory leaks;
One-time memory leak;
Implicit memory leak.

  1. 1   void heap-leap_test
  2. 2 {
  3. 3       char *p = ( char *)malloc(sizeof( char ));
  4. 4       //exe task  
  5. 5 free(p); //If the memory space pointed to by pointer p is not released, memory leak will occur.  
  6. 6 p = NULL; //If the pointer p is not set to NULL, p will become a wild pointer.  
  7. 7 }

<<:  7 challenges facing Apple Watch app developers

>>:  FB open-sources React Native, using JS to develop native iOS apps

Recommend

Xiao Fei Da Xia Training Camp Episode 9 Baidu Cloud Download

Resource introduction of the ninth phase of the L...

Comprehensive analysis of iOS unit testing and UI testing

【51CTO.com Quick Translation】Writing tests is not...

Camels are worthy of their title as the "kings of the desert"

In nature, even elephants weighing 5 tons will be...

There are only two types of marketers: experts and operators!

I have read an article published by Diao Ye befor...

A detailed discussion on short video operation strategies (Part 2)

Preface: In the previous article, the author disc...

India bans 59 Chinese apps; experts: limited impact

On June 30, India banned 59 Chinese apps for secu...

Japan can no longer print money? It turns out that prices have increased...

Is Japan no longer able to print money? Next year...

Marketing promotion routines leveraging the Qixi Festival!

The annual Chinese Valentine's Day is approac...

What’s the secret to “brand placement” that goes viral?

There is a saying in the branding industry that “...

17 Swift Best Practices

[[151452]] Preface This post is a compilation of ...

Amid the proliferation of copycat giants: A survey of the domestic VR ecosystem

Introduction It is worth noting that BAT has also...