Turn from: http://blog.csdn.net/feixiaoxing/article/details/6746335
In our personal programming process, memory leaks, although not like the memory overflow caused a variety of inexplicable strange problems, but its harm can not be ignored. On the one hand, the memory leakage causes our software to occupy the more and more memory in the operation process, occupies the resources but does not have the time to clean up, this will cause our procedure the efficiency to be lower; on the other hand, it may affect our user's experience, loses the market competition ability.
A common memory leak is this:
View plain void process (int size) {char* PData = (char*) malloc (size); /* Other code */return; /* Forget to free pData/} as shown in the figure above, we need to request memory for each time we process the function, but there is no release at the end of the function. If such a piece of code appears on the business side, then the consequences are unimaginable. For example, if our server needs to accept concurrent access for 100 users per second, data from each user, we need to save a copy of the local application memory. After processing is finished, if the memory is not released well, it will result in less physical memory available to our server. Once a certain critical point is reached, the operating system has to meet our requirements for new memory through the internal and external scheduling, which on the other hand reduces the quality of Server service.
The dangers of memory leaks are self-evident, but finding memory leaks is a difficult and complex task. We all know that fixing bugs is a very simple thing to do, but finding the source of a bug is a hard task. Therefore, it is necessary for us to write the code, the search for memory leaks in a very important position. So is there any way to solve the problem?
I want to do to solve the memory leak, must do the following two aspects:
(1) must record the memory in which function request, the specific file number of rows is
(2) When should memory be released
It is not difficult to complete the 1th condition. We can use the node method to record the memory we are applying for:
A) set up the data structure of the node
View plain typedef struct _MEMORY_NODE {char functionname[64]; int line; void* paddress; struct _memory_node* next; }memory_node; Where the functionname record function name, line record row number, Paddress record assigns the address, next records the next memory node.
b Modify the memory allocation function
Modify the malloc on the business side to add the following macro statement
#define MALLOC (param) memorymalloc (__function__, __line__, param)
Write the following code on the side of the pile function
View Plain