Finding and fixing memoy leaks in your software

Memory leaks will cause your device to crash after a period of time once it runs out of memory.
A quick way to find out if your application has a memory leak(s) is to monitor it with top:


Memory leak
Not the way to fix a leak…

If you see the %MEM increase over time for no particular reason, then you’ve got a memory leak.

However, it might be tricky to isolate where the issue occurs exactly.

The first thing to do is the review your source code for the following:

  1. Malloced memory is always freed
  2. fopen is always followed by fclose, and open by close
  3. scandir calls are properly freed
  4. Threads are properly terminated with pthread_cancel & pthread_join or pthread_detach, etc…

If after a code review you cannot find the reason for the memory leak, use the following piece of code:


 

The memory usage is displayed in numbers of pages. Usually, one page is 4096 bytes but it obviously depends on your system.

After you added this debug code,  insert the macro DISPLAY_MEMORY_USAGE at strategic places in your code (e.g. before a thread starts and after it stops) to find out where the memory leak occurs.  Then run the code and check the log to locate the issue.

Once you’ve located where the memory leak occurred, narrow down your search by adding more DISPLAY_MEMORY_USAGE in your code and remove the unused ones until you finally pinpoint the exact place in your code and resolve the issue.

Share this:
FacebookTwitterHacker NewsSlashdotRedditLinkedInPinterestFlipboardMeWeLineEmailShare

Support CNX Software! Donate via cryptocurrencies, become a Patron on Patreon, or purchase goods on Amazon or Aliexpress

ROCK 5 ITX RK3588 mini-ITX motherboard

Radxa ROCK 5C (Lite) SBC with Rockchip RK3588 / RK3582 SoC

3 Replies to “Finding and fixing memoy leaks in your software”

  1. I have already located the area of code where memory is not freeing, though I am not allocating any dynamic memory in my code, still I have don’t know what is the exact reason for memory leak??

  2. @nitin Kumar
    Some standard C functions allocate memory for you, and if you don’t call the right functions to free memory, a memory leak may occur. This happen to me once with directory listing (with dirent.h, I think). I did not call malloc or free directly, but these functions did, and I did not do the clean up correctly.

  3. thanks, let me explain you in detail about the issue, We are doing FTP from one unit to another, This we are doing step by step so after receiving file in one module I am transferring it to next module right after which I am deleting the file though file system is showing file is not there but there is no difference in free memory before and after deletion. And after a few iterations it is showing “double free or corruption” and application is terminating. Moreover I have tried with a test application and repeated same procedure on same board that time it is working perfectly.

Leave a Reply

Your email address will not be published. Required fields are marked *

Boardcon Rockchip and Allwinner SoM and SBC products
Boardcon Rockchip and Allwinner SoM and SBC products