Using valgrind to detect memory leaks
Valgrind is a powerful tool to detect potential memory leaks and understand memory profile of a process.
Following is an example to start a program under Valgrind to detect leaks and analyze memory profile:
valgrind --log-file=process_mem.log </path-of-binary-to-analyze> -N
Example: Creating a memory leak in C program.
In the following C code, the address of “newName” is assigned to “name”. So, we lost the original address of “name”.
Even we do free(name), we can’t free 20 bytes since we are actually freeing memory at “newName”. So we successfully created a memory leak.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include<stdio.h> #include<stdlib.h> int main() { // Initialise two character pointers char *name; char *newName; //Allocate memory name = malloc(20); newName = malloc(20); printf("name reference is %ld \n",name); printf("newName reference is %ld \n",newName); //Assign newName address to name. name = newName; // free memory free(name); free(newName); printf("name reference is %ld \n",name); printf("newName reference is %ld \n",newName); return 0; } |
Detecting memory leaks using Valgrind
I am now compiling my C code and running it through Valgrind to find out memory leaks. Following is the output.
You can notice that valgrind reported “definitely lost: 20 bytes in 1 blocks”
[root@electronproton ~]# valgrind ./mem_leak_test.o ==6740== Memcheck, a memory error detector ==6740== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. ==6740== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info ==6740== Command: ./mem_leak_test.o ==6740== name reference is 85934144 newName reference is 85934240 ==6740== Invalid free() / delete / delete[] / realloc() ==6740== at 0x4C28CDD: free (vg_replace_malloc.c:530) ==6740== by 0x40062C: main (in /root/mem_leak_test.o) ==6740== Address 0x51f40a0 is 0 bytes inside a block of size 20 free'd ==6740== at 0x4C28CDD: free (vg_replace_malloc.c:530) ==6740== by 0x400620: main (in /root/mem_leak_test.o) ==6740== Block was alloc'd at ==6740== at 0x4C27BE3: malloc (vg_replace_malloc.c:299) ==6740== by 0x4005DC: main (in /root/mem_leak_test.o) ==6740== name reference is 85934240 newName reference is 85934240 ==6740== ==6740== HEAP SUMMARY: ==6740== in use at exit: 20 bytes in 1 blocks ==6740== total heap usage: 2 allocs, 2 frees, 40 bytes allocated ==6740== ==6740== LEAK SUMMARY: ==6740== definitely lost: 20 bytes in 1 blocks ==6740== indirectly lost: 0 bytes in 0 blocks ==6740== possibly lost: 0 bytes in 0 blocks ==6740== still reachable: 0 bytes in 0 blocks ==6740== suppressed: 0 bytes in 0 blocks ==6740== Rerun with --leak-check=full to see details of leaked memory ==6740== ==6740== For counts of detected and suppressed errors, rerun with: -v ==6740== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) [root@electronproton ~]#
Hope you loved it 🙂
Installing valgrind on Linux
I just installed Valgrind on my system using yum. Just use the following command to install valgrind.
[root@electronproton ~]# yum install valgrind.x86_64 Loaded plugins: fastestmirror, langpacks c7-media | 3.6 kB 00:00:00 Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package valgrind.x86_64 1:3.11.0-24.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ========================================================================================================= Package Arch Version Repository Size ========================================================================================================= Installing: valgrind x86_64 1:3.11.0-24.el7 c7-media 6.4 M Transaction Summary ========================================================================================================= Install 1 Package Total download size: 6.4 M Installed size: 26 M Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : 1:valgrind-3.11.0-24.el7.x86_64 1/1 Verifying : 1:valgrind-3.11.0-24.el7.x86_64 1/1 Installed: valgrind.x86_64 1:3.11.0-24.el7 Complete!
Enjoy memory debugging using Valgrind. I will update this article with more potential examples soon.