Saturday, February 15, 2014

Memory Leak Basics & Impact


In the past, I developed a few useful tools for detecting memory leaks caused by linux kernel modules.

In this post, I will go over the basics of a memory leak and the impact/consequences of a memory leak on an embedded system.

Basics

Here is a sample C program that allocates a character array, but doesn't free it:
#include <stdio.h>
 
void alloc_function();
 
int main(void)
{
   alloc_function();
 
   return 0;
}
 
void alloc_function(void)
{
   char *ptr; 
 
   ptr = (char *) malloc (10);
   ptr[0] = 10; 
 
   return;
   // ptr is not freed before returning.. this causes memory leak
 
}

Memory Leak Consequences

A memory leak in a rarely executed code path may not be a serious issue. 

However, when frequently executed code paths leak memory over a period of time continuously, memory available to the system goes down and hence memory leak can be harmful. Memory leaks can cause software:

  • to perform poorly by reducing the amount of memory available in the system
  • to malfunction due to memory allocation errors 
  • to eventually bring down the entire system


No comments:

UA-48797665-1