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.
Unreachable. The program no longer holds any pointer to the block, a pointer being a value that holds a memory address. The last pointer was overwritten, or it was a local variable of a function that has returned. The program can no longer free the block.
Reachable but useless. The program still holds a pointer, usually inside a list or table that only grows, and will never use the block again.
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.
Define a cycle: one complete unit of the repeated work, such as one request served or one update applied.
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.
Run many cycles, and include cycles that fail and cycles that are canceled.
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.
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 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.
Version 1: after cycles 1 to 5 the values are 16, 32, 48, 64 and 64 bytes. After 10 cycles and after 20 cycles the value is still 64 bytes.
Version 2: every value is higher than in version 1 by 16 bytes for each completed cycle. After cycles 1 to 5 the values are 32, 64, 96, 128 and 144 bytes. After 10 cycles the value is bytes, and after 20 cycles it is bytes.
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 bytes and version 2 grows by 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 bytes and leaves bytes. Each cycle of version 2 loses 24 bytes, and . So 166 cycles succeed, and the allocation in cycle 167 fails, because only 16 bytes remain.
Stated precisely
Let be the number of bytes of the pool that are in use at the end of cycle , measured when no work is in progress. The measured paths have no repeating leak when is bounded: there is a fixed number , the same for every number of cycles, with for every . The program has a repeating leak when keeps growing with and would pass any fixed number if the pool were large enough. A cache with a bound adds at most its bound to .
For a steady leak of bytes per cycle, once every cache is full, where is the constant amount held by the full caches and other long-lived data. With a pool of bytes, an allocation fails in the first cycle for which . In the example , and : does not exceed 4096 and 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].