Unix Reference

valgrind (runs your program and finds memory leaks and memory errors)

Video

valgrind is a program that runs your program and determines if it has memory leaks, or if there are any other memory-related issues. You will used valgrind extensively in cs107.

Example:

$ cat memoryLeak.c
#include<stdlib.h>
#include<stdio.h>
#include<time.h>

const int ARR_SIZE = 1000;

int main() {
    // create an array of ARR_SIZE ints
    int *intArray = malloc(sizeof(int) * ARR_SIZE);

    // populate them
    for (int i=0; i < ARR_SIZE; i++) {
        intArray[i] = i;
    }

    // print one out
    // first, seed the random number generator
    srand(time(NULL));
    int randNum = rand() % ARR_SIZE;

    printf("intArray[%d]: %d\n", randNum, intArray[randNum]);

    // end without freeing!
    return 0;
}

$ gcc -g -Og -std=gnu99 memoryLeak.c -o memoryLeak
$ ./memoryLeak
intArray[857]: 857
$ valgrind ./memoryLeak
==12291== Memcheck, a memory error detector
==12291== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==12291== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==12291== Command: ./memoryLeak
==12291==
intArray[61]: 61
==12291==
==12291== HEAP SUMMARY:
==12291==     in use at exit: 4,000 bytes in 1 blocks
==12291==   total heap usage: 1 allocs, 0 frees, 4,000 bytes allocated
==12291==
==12291== LEAK SUMMARY:
==12291==    definitely lost: 4,000 bytes in 1 blocks
==12291==    indirectly lost: 0 bytes in 0 blocks
==12291==      possibly lost: 0 bytes in 0 blocks
==12291==    still reachable: 0 bytes in 0 blocks
==12291==         suppressed: 0 bytes in 0 blocks
==12291== Rerun with --leak-check=full to see details of leaked memory
==12291==
==12291== For counts of detected and suppressed errors, rerun with: -v
==12291== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
$

The program memoryLeak.c requests memory from the heap and then ends without freeing the memory. This is a memory leak, and valgrind correctly finds the leak: "definitely lost: 4,000 bytes in 1 blocks"

If you want more information, you can follow the instruction to run valgrind with the --leak-check=full flag:

$ valgrind --leak-check=full ./memoryLeak
==12390== Memcheck, a memory error detector
==12390== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==12390== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==12390== Command: ./memoryLeak
==12390==
intArray[379]: 379
==12390==
==12390== HEAP SUMMARY:
==12390==     in use at exit: 4,000 bytes in 1 blocks
==12390==   total heap usage: 1 allocs, 0 frees, 4,000 bytes allocated
==12390==
==12390== 4,000 bytes in 1 blocks are definitely lost in loss record 1 of 1
==12390==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12390==    by 0x400677: main (memoryLeak.c:9)
==12390==
==12390== LEAK SUMMARY:
==12390==    definitely lost: 4,000 bytes in 1 blocks
==12390==    indirectly lost: 0 bytes in 0 blocks
==12390==      possibly lost: 0 bytes in 0 blocks
==12390==    still reachable: 0 bytes in 0 blocks
==12390==         suppressed: 0 bytes in 0 blocks
==12390==
==12390== For counts of detected and suppressed errors, rerun with: -v
==12390== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

Because we compiled with the -g flag, valgrind is able to tell us exactly where in our program the leak was created:

==12390==    by 0x400677: main (memoryLeak.c:9)

In other words, on line 9 in memoryLeak.c, we allocated the memory and it was never freed. This can be super-helpful when debugging your programs!

Back to contents