Memory leakage and a simple detection method, memory leakage detection

Source: Internet
Author: User

Memory leakage and a simple detection method, memory leakage detection
1. What is Memory Leak )? Simply put, a memory space is applied for and is not released after use. Generally, the longer the program runs, the more memory it occupies, and the entire system crashes. A piece of memory applied by the program, and no pointer points to it, the memory will be leaked. 2. Hazard of Memory leakage

From the perspective of user programs, memory leakage does not cause any harm. As a general user, the memory leakage does not exist. The real danger is the accumulation of Memory leakage, which will eventually consume all the memory of the system. There are mainly the following forms:

1) cpu resource depletion: it is estimated that the machine has no response, keyboard, mouse, and network. This is often seen on windows, especially in the form of viruses. 2) process id depletion: No way to create a new process, no way to create a serial port or telnet. 3) Hard Disk depletion: The machine is dying, the swap memory is useless, and the log is useless. It is normal to die.

When writing a program, we usually use functions such as malloc, realloc, and new to allocate a block of memory from the heap, the program must call free or delete to release the memory block. Otherwise, the memory cannot be used again, so we can say that the memory is leaked. To avoid this problem, we should start with the Code. Good coding habits and specifications can avoid errors.

3. How to detect memory leakage?

First: Good coding habits, try to detect memory leakage in the memory-related segments. When the program is stable, it will undoubtedly increase the difficulty and complexity of troubleshooting when detecting memory leaks. The memory allocation function is used. Once used, remember to use the corresponding function to release it.

Heap memory:

Malloc \ realloc ------ free

New \ new [] ---------- delete \ delete []

GlobalAlloc ------------ GlobalFree

Pay special attention to the memory leakage of array objects.

MyPointEX * pointArray = new MyPointEX [100];

The deletion form is:

Delete [] pointArray;


2. Manage the allocated memory pointer in the form of a linked list. after use, delete the pointer from the linked list. When the program ends, check and modify the linked list.

Third: smart pointer in Boost.

Fourth: some common tool plug-ins, such as ccmalloc, Dmalloc, and Leaky.

4. Sample Code
I mainly want to talk about the second method in combination with the Code. The design idea is actually very simple and uses the list in STL. Someone may ask, why not use a vector?
The differences between list and vector are as follows:

Vector allocates a continuous address space for the stored objects. Therefore, it is highly efficient to randomly access elements in the vector. To insert or delete an element in the vecotor, you must copy and move the existing element. If the objects stored in the vector are large, or the constructor is complex, the overhead of copying existing elements is large, because the copy constructor must be called to copy objects. For simple small objects, vector is more efficient than list. Each time a vector expands its capacity, it doubles the capacity, which is highly efficient for small objects.

Objects in the list are stored discretely. to randomly access an element, You need to traverse the list. It is highly efficient to insert elements into the list, especially at the beginning and end of the list,
You only need to change the element pointer.

Applicable to vector: the number of objects changes little, simple objects, and frequent Random Access to elements

Applicable to list: large changes in the number of objects, complex objects, and frequent insertion and Deletion

1 # include <iostream> 2 # include <stdio. h> 3 # include <stdlib. h> 4 # include <string. h> 5 # include <list> 6 7 using namespace std; 8 9 const int nMaxSize = 26; 10 struct Node 11 {12 int count; 13 Node * next [nMaxSize]; 14}; 15 Node * root = NULL; 16 list <Node *> nodeMemory; 17 18 void TreeCreate () 19 {20 root = (Node *) malloc (sizeof (Node); 21 for (int I = 0; I <nMaxSize; ++ I) 22 {23 root-> next [I] = NULL; 24} 25} 26 27 void TreeInsert (char * pStr) 28 {29 int I, j, len = strlen (pStr); 30 Node * p = root; 31 Node * q = NULL; 32 for (I = 0; I <len; ++ I) 33 {34 int id = pStr [I]-'A '; 35 if (p-> next [id] = NULL) 36 {37 q = (Node *) malloc (sizeof (Node); 38 if (q! = NULL) 39 nodeMemory. push_back (q); 40 q-> count = 0; 41 for (j = 0; j <nMaxSize; ++ j) 42 {43 q-> next [j] = NULL; 44} 45 p-> next [id] = q; 46} 47 p-> next [id]-> count ++; 48 p = p-> next [id]; 49} 50} 51 52 int TreeQuery (char * pStr) 53 {54 int I, len = strlen (pStr); 55 int id = 0; 56 Node * p = root; 57 for (I = 0; I <len; ++ I) 58 {59 id = pStr [I]-'A'; 60 p = p-> next [id]; 61 if (p = NULL) return 0; 62} 63 return p-> count; 64} 65 66 void TreeDelete (Node * p) 67 {68 if (p = NULL) 69 return; 70 for (int I = 0; I <nMaxSize; ++ I) 71 {72 TreeDelete (p-> next [I]); 73} 74 nodeMemory. remove (p); 75 free (p); 76 p = NULL; 77} 78 79 int main (int argc, char ** argv) 80 {81 char szBuffer [16]; 82 int res = 0; 83 TreeCreate (); 84 int n = 3; 85 while (n) 86 {87 gets (szBuffer); 88 if (strlen (szBuffe R) = 0) 89 break; 90 TreeInsert (szBuffer); 91 n --; 92} 93/* scanf ("% s", szBuffer ); 94 res = TreeQuery (szBuffer); 95 printf ("% d \ n", res); */96 for (list <Node * >:: iterator it = nodeMemory. begin (); it! = NodeMemory. end (); it ++) 97 {98 cout <* it <endl; 99} 100 cout <nodeMemory. size () <endl; 101 TreeDelete (root); 102 if (nodeMemory. empty () 103 cout <"has delete the tire tree" <endl; 104 cout <nodeMemory. size () <endl; 105 system ("pause"); 106 return 0; 107}View Code

The code is very simple. The list stores the pointer to the memory space. After each malloc operation, the allocated memory pointer is pushed to the list. After free, list will delete the corresponding content. If all the content is released, the list will become empty, so that you can determine whether all the memory is released after use?

5. Last thought

The first time I posted a blog on the blog site, I found that sina was not very bad in order to facilitate my blog writing in sina. So I moved here to write a new blog. I hope you will not be enlightened.

Blog reference link:

Http://blog.csdn.net/na_he/article/details/7429171

Http://baike.baidu.com/view/1068433.htm

Http://blog.csdn.net/renkaihao/article/details/6803866

 

 

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.