Section 4.8

Memory leak

Definition

A memory leak is memory that a program has allocated, meaning reserved for its own use while it runs, and that it no longer needs and never releases. The leaked memory stays reserved, so the program cannot use it for anything else. A leak becomes a fault when it repeats: every request, connection or update leaves a little more memory behind, and in the end a request for memory fails. A cache, memory kept on purpose to make later work faster, differs by having a fixed limit.

The idea

C does not release by itself the memory that a program requests while it runs. A program obtains a block from the allocator, the library code that manages such memory, with the function malloc, and gives the block back with the function free. A leak is a missing free. It often sits on a path that runs rarely, such as the handling of an error: the function allocates a buffer, meets a problem, returns early and skips the line that frees the buffer.

A restaurant that never clears its tables gives a fair picture. Each party is served correctly and leaves its dishes behind, the tables fill up, and one evening no clean table is left for the next guests. The picture stops being accurate in one respect. Dirty tables can be seen. Leaked memory is invisible until the moment a request for memory is refused.

How it works

A leaked block is in one of two states.

A cache resembles the second state. A cache is memory kept on purpose so that later work is faster. Three properties separate a cache from a leak: a bound, meaning a stated maximum size and a rule for what to discard when the maximum is reached; reuse, meaning that later work actually uses what the cache holds; and an owner, a part of the program that is responsible for the cache and can empty it. A list or table that is called a cache but has no bound is a leak.

Measurement separates the two as follows.

  1. Define a cycle: one complete unit of the repeated work, such as one request served or one update applied.

  2. At the same point of every cycle, when no work is in progress, record the number of bytes that are currently allocated, as counted by the allocator.

  3. Run many cycles, and include cycles that fail and cycles that are canceled.

  4. Read the series of numbers. A cache makes the number rise during the first cycles and then stay level. A leak makes the number rise for as long as the cycles continue.

  5. Compare the level value with the bound that the design states.

Worked example

A program serves requests. It has a cache of at most 4 buffers of 16 bytes each, so the bound of the cache is 4×16=644 \times 16 = 64 bytes. Each cycle adds one buffer to the cache until the cache is full. Version 1 of the program has no leak. Version 2 also loses one block of 16 bytes in every cycle. The number of bytes in use after each cycle is as follows.

A test of 4 cycles shows growth in both versions, so growth during the first cycles does not reveal the leak. From cycle 10 to cycle 20, version 1 grows by 64−64=064 - 64 = 0 bytes and version 2 grows by 384−224=160384 - 224 = 160 bytes, which is 16 bytes for each of the 10 cycles.

Suppose the allocator of a device serves every request from a fixed pool of 4096 bytes and needs 8 bytes of its own records for each block, so that every block of 16 bytes costs 24 bytes of the pool. The full cache costs 4×24=964 \times 24 = 96 bytes and leaves 4096−96=40004096 - 96 = 4000 bytes. Each cycle of version 2 loses 24 bytes, and 4000=24×166+164000 = 24 \times 166 + 16. So 166 cycles succeed, and the allocation in cycle 167 fails, because only 16 bytes remain.

Stated precisely

Let M(c)M(c) be the number of bytes of the pool that are in use at the end of cycle cc, measured when no work is in progress. The measured paths have no repeating leak when M(c)M(c) is bounded: there is a fixed number BB, the same for every number of cycles, with M(c)≤BM(c) \le B for every cc. The program has a repeating leak when M(c)M(c) keeps growing with cc and would pass any fixed number if the pool were large enough. A cache with a bound adds at most its bound to BB.

For a steady leak of bb bytes per cycle, M(c)=M0+b×cM(c) = M_0 + b \times c once every cache is full, where M0M_0 is the constant amount held by the full caches and other long-lived data. With a pool of HH bytes, an allocation fails in the first cycle cc for which M0+b×c>HM_0 + b \times c > H. In the example M0=96M_0 = 96, b=24b = 24 and H=4096H = 4096: 96+24×166=408096 + 24 \times 166 = 4080 does not exceed 4096 and 96+24×167=410496 + 24 \times 167 = 4104 does, so cycle 167 is the first to fail.

Growth that continues after every cache is full shows a leak. A level series shows only that there is no leak on the paths and inputs that the test ran.

Limits and common mistakes

The size of the process is the wrong measure. A process is a running program as the operating system sees it. The manual of glibc, the library that supplies malloc and free on many Linux systems, says that free can only occasionally return memory to the operating system and usually just makes the space available to a later call of malloc. A process that does not shrink after freeing memory is therefore not shown to be leaking.

Tools that look for lost pointers miss the second state. Memcheck, a tool of the Valgrind tool set for examining programs while they run, reports a block as definitely lost when it can find no pointer to the block. A block that a growing list still points to is classed as still reachable and is not listed by default. A garbage collector, a part of some languages that frees blocks to which no pointer leads, does not free such a block either.

Overwriting the only pointer loses the block. The statement p = realloc(p, n) is a well-known case. When realloc, the function that changes the size of a block, fails, it returns a null pointer §4.26, a value that points to no object, and leaves the old block allocated. The assignment then destroys the only pointer to that block.

A leak can be an attack. Someone who can trigger the leaking path at will from outside can use up the memory of the device. That is a denial of service §1.28: the device stops doing its work.

Questions answered

How do you distinguish a cache from a leak across many cycles?

By whether the memory in use stops growing at a limit that the design states. A cache is memory kept on purpose so that it can be used again, with a fixed maximum size. A memory leak is memory that is no longer needed and never released. Record the number of allocated bytes at the same quiet point of every cycle, where a cycle is one complete unit of the repeated work. Run enough cycles to fill every cache, and include cycles that fail or are canceled. A cache makes the number rise during the first cycles and then stay level at or below its maximum. A leak makes the number rise for as long as the cycles continue.

Take the number from the allocator, the library code that hands out memory, and not from the amount of memory that the operating system reports for the running program, because freed memory usually stays with the program. Measure when no work is pending, so that the buffers of requests in progress are not counted as leaked. Treat a level series as evidence only for the paths that the test ran. A structure that is called a cache but has no maximum size is a leak.

Related entries: Ownership (§4.2), Heap fragmentation (§4.9), Resource confinement (§2.40), Double free (§4.7), Denial of service (DoS) (§1.28), Memoization and caching (§14.20). Further reading: [78].