Memory leak problem is C language is very easy to appear problems, small programs can be easily found, but the large program is more difficult to find.
The memory leak is because the dynamically allocated memory is not released and can be checked out using tools such as Valgrind.
The functions that often lead to memory leaks are malloc, calloc, and indirectly using malloc functions StrDup, Strndup, and so on. Also includes the Mmap function.
To avoid memory leaks, you need the following functions to appear in pairs:
Malloc/free
Calloc/free
Strdup/free
Strndup/free
Mmap/munmap
< after encountering the pit, continue to add >
The problem I encountered was a memory leak, but Valgrind did not find out that malloc was not released. The result is that Mmap is not released.
$ valgrind--leak-check=yes--leak-check=full--show-leak-kinds=all./bin/ap_collector > Lost.log 2>&1
The results of the analysis are shown below:
Although no memory leaks were found, memory continues to grow rapidly. Later I analyzed:
1, the memory continues to grow: positioning basically occurs within the while loop.
2, memory block speed growth: indicating that a large number of memory blocks were applied, not released.
3. No malloc and StrDup memory leaks: Explains the need to see if other functions have large chunks of memory to request.
So based on the above analysis, check my cyclic processing logic code, found that the memory of the mmap application forgot to release, add Munmap, memory leak is resolved.
Record the pits you've trod and accumulate experience.