Analysis of memory overflow and Memory leakage

Source: Internet
Author: User

Analysis of memory overflow and Memory leakage

In actual programming, we often talk about memory overflow and Memory leakage, especially for C/C ++ programs, the following code examples are C/C ++ ), this is because we will directly deal with memory. However, in many cases, we cannot fully understand these two concepts, and sometimes even confuse them upside down.

In fact, we can also understand the memory overflow and Memory leakage in terms of naming, for example, it may not be appropriate. For example, it is to fetch water in the water tank. Originally, this tank can only hold 5 barrels of water. After 5th barrels are installed, you have to hold 6th barrels of water. Naturally, the water in the tank overflows, this is "memory overflow". No one uses the tank after it is filled with water. The second day, I found that the water in the cylinder was half less, and the third day there was no drops left, it turns out that I made a hole in the bottom of the cylinder and forgot to fill it out. Why do I need a hole in the bottom of the cylinder? No, it's fun and willful ). This is like applying for a piece of memory and forgetting to release it, resulting in "Memory leakage ". The two concepts are briefly described below.

First, let's talk about memory overflow. memory overflow simply means that the memory allocated exceeds what the system can provide. For example, if you have applied for a 10-byte memory space, you just need to compress 11 bytes of data in it, and it will naturally be full. It overflows, for example, sample code 1 ). In fact, array out-of-bounds is also a kind of memory overflow. For example, when writing data beyond the array range, reading the array data is not counted as memory overflow ). After the array is filled out of the bounds, if it is inserted in, it will occupy the stack memory. Generally, the array is declared as a local variable, and the local variable is automatically allocated in the stack area ). The out-of-bounds part is used as a local variable to occupy the stack memory, because the stack is from the bottom of the RAM to survive the data), and other data running the program is from the top down, therefore, when the stack stores more data and the accumulation increases, the stack will meet the data during the running of the program. The two occupy the entire RAM memory, and the stack will continue to consume, the stack grows up and overwrites the variables required for running the program, and the program is about to run. In this case, the memory overflow is terrible.

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

Next, let's talk about memory leakage. Generally, memory leakage refers to Heap memory leakage (Heap leak). After dynamically applying for memory on the stack, the memory leakage will occur if it is not released in time. By the way, if the pointer memory is released but the pointer is not immediately set to NULL, it will cause a wild pointer, for example, sample code 2 ). A memory leak may not be noticed, and may not cause any harm. However, the accumulation of Memory leakage will cause the memory to be used up, and the consequences will be serious. For example, memory leakage occurs in the loop body. Of course, there are other forms of Memory leakage, such as memory leakage caused by Resource leak. According to the frequency of Memory leakage, memory leakage can be divided into the following four types:
Frequent Memory leakage;
Occasional Memory leakage;
One-time memory leakage;
Implicit Memory leakage.

 
 
  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 pointer p is not released, the memory will leak.
  6. 6 p = NULL; // if the pointer p is not set to NULL, p will become a wild pointer.
  7. 7}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.