CSE Roadmap

Core tier · about 5 hours · 7 parts

Part 3 of 7 · 2 min read

Memory leaks

In plain words: A leak is a block that is never freed, and that no pointer can reach any more. It does not cause an error. It causes growth. The program below handles "requests" and forgets to free a 1 MiB buffer in each:

static void handle_request(void){
  char *buf = malloc(1 << 20);
  memset(buf, 1, 1 << 20);        /* forgot free(buf) */
}

After a number of requests, the resident size (RSS) is printed:

Requests RSS
25 27,304 kB
50 53,192 kB
75 78,892 kB
100 104,592 kB

The growth is steady: about 1,030 kB for each request. A server with this bug would use more memory each hour, until the system runs short and the kernel kills it (page 7 of this level). A leak of a few bytes for each request may take days to matter, which is why leaks are found late.

To find the line that allocates the lost memory, run the program under a leak checker. Valgrind's report for the same program, with --leak-check=full, names the allocating call:

16 bytes in 1 blocks are definitely lost in loss record 1 of 1
   at 0x4846828: malloc (...vgpreload_memcheck-amd64-linux.so)
   by 0x109208: main (bugs.c:6)

That line is from the small test of section 1 (case 4). On the request handler above, the summary said 102,760,448 bytes in 98 blocks were definitely lost, and 2,097,152 bytes in 2 blocks were possibly lost (stale copies of the last two pointers were still on the stack, so the tool could not be sure).

Memory leak

A memory leak is a block of heap memory that the program has allocated, no longer needs, and cannot free because it holds no pointer to it any more, or that it simply never frees, so the memory use of the process grows over time.

Example: a request handler that allocates a 1 MiB buffer and returns without calling free leaks 1 MiB for each request.

Check yourself: 1. How does a leak show itself in the output of top or /proc/PID/statm? 2. What is the difference between "definitely lost" and "possibly lost" in the report? 3. Why are leaks often found late?