Home
Netbeans Eclipse Qt Java
Games
College of Engineering Aeronautics and Astronautics Agricultural and Biological Engineering Biomedical Engineering Chemical Engineering Civil Engineering Construction Engineering and Management Electrical and Computer Engineering Engineering Education Engineering Professional Education Environmental and Ecological Engineering Industrial Engineering Materials Engineering Mechanical Engineering Nuclear Engineering
EPICS (Engineering Projects In Community Service) First-Year Engineering Program First-Year Engineering Honors Program Global Engineering Program Minority Engineering Program Professional Practice (Co-Op) Program Women in Engineering Program
College Administration Schools Programs All Groups All People ECN Webmail
Purdue Home

ECE 264 Exercise 5

Memory Leak

 

By now, you should know that using a pointer before allocating memory can terminate your program with a message "Segmentation fault."  There is another type of memory problem, called memory leak. It means the program does not release unused memory. A C program has to handle both allocation and release. In some programming languages, such as Java, programs need to allocate memory and unused memory is automatically released by the languages. This is called garbage collection.  Whether a programming language should provide garbage collection has been a topic of debate for decades. C does not provide garbage collection.

Memory Leak

The following simple program shows what is memory leak.

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{
int * dataArray;
dataArray = (int *) malloc(10 * sizeof(int));
return 0;
}

This program allocates memory for dataArray but does not release it.  In this example, if an integer takes 4 bytes, the program leaks 40 bytes. The program should add

 free(dataArray);

before it terminates. Why is this a problem? The program terminates and the operating system will reclaim the memory occupied by this program anyway. This is correct. However, many programs do not terminate. They can run for days, months, or years, for example, e-commerce or traffic control.  If a program gradually leaks memory, the program can cause serious problems.

  • If the program runs on a computer with virtual memory, the program can gradually become slower as more and more memory is used and swapped to secondary storage.
  • Even if the program leaks only a few bytes each minute, eventually the program will run out of memory and the program will terminate (i.e. crash).

Memory leak is a silent killer of programs because the programs do not crash right away.  Instead, the programs can run for hours or days without any visible problem. All of sudden, the programs run out of memory and crash. As software is adopted in many critical systems (traffic control, medical...), an unexpected termination can cause serious damage.

Detect Memory Leak

Fortunately, many tools are available to detect memory leaks. This exercise asks you to use valgrind. Save the above program (without free) and called it exercise5.c. Create the executable using the following command.

               gcc exercise5.c -o exercise5

If you execute the program, it terminates without any message. Execute the program again using the following command

              valgrind  --leak-check=yes  ./exercise5

Some messages appear on the screen. Among them,

    LEAK SUMMARY:
    definitely lost: 40 bytes in 1 blocks.

This tells us the program leaks memory. Next, change the program

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{
int * dataArray;
dataArray = (int *) malloc(10 * sizeof(int));
free(dataArray);
return 0;
}

Execute the program again with valgrind. It does not report memory leak any more.

What to Submit?

The outputs of running valgrind twice, the first without free and the second with free.