I encountered Linux under the C language memory leak summary __oracle

Source: Internet
Author: User
Tags valgrind

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.

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.