Memory Leaks

Learn what a memory leak is, its symptoms, and its causes.

An unhandled error condition leads to a crash immediately. A program will hang as soon as it starts waiting for a condition (that goes unfulfilled eventually). Unlike these two, the effect of a resource leak on a program can remain undetected for quite some time. The ultimate result of a resource leak on a product and the environment where it runs could vary based on the platform. A variety of platform-specific tools are needed to go after them. In this lesson, we’ll learn about memory and resource leaks in general. We’ll start with memory leaks, which are by far the most commonly seen among all resource leaks.

What is a memory leak?

Even the most straightforward software program has to handle memory in the form of variables which are instantiated data types or data structures. The data types a programming language offers are typically basic, like integer, character, float, etc., and the compounded ones are composed of multiple instances of these basic types, like arrays, lists, etc. The compounded data types can be of any length the runtime environment allows. The length is usually known at compile-time for data types like arrays, but for lists, etc., the size is completely unbounded.

Apart from data structures and data types, platforms provide their programs with a dynamic way to allocate memory. At runtime, we can specify the amount of memory to allocate and create a function to free up or release the memory allocated.

The program must invoke a function to return this memory allocated to the platform. Some programming languages offer functions to allocate and deallocate memory directly to the programmer. These functions invoke a system call to the underlying operating system, which is typically expensive.

To avoid these frequent system calls, programming languages employ schemes and strategies that typically allocate more memory than asked for by the program. They also give out memory to and from this preallocated buffer and make further system calls only when they can no longer give out memory from this buffer.

Some programming languages abstract the underlying memory away from the programmer and handle dynamically allocated memory themselves. Both strategies have advantages and disadvantages. While allowing programmers to invoke the allocation functions directly provides increased flexibility, it creates problems when or if the free function is not invoked.

On the other hand, garbage collection is not “free.” It comes with increased overhead and can cause performance issues.

So, memory is a very critical resource a process uses. A process is said to leak memory if it uses more memory than it needs. This broad definition includes different underlying reasons with subtle differences, which we will elaborate on. 

What causes a memory leak?

From the code perspective, memory leaks in programs can be caused by two scenarios:

When a programmer calls the function to allocate memory and does not call the function that releases the allocated memory back to the system. This is applicable in programming languages that offer such low-level functions to allocate and release memory. For example, consider the following (very simple) C code:

Get hands-on with 1400+ tech skills courses.