Section 18.5.11

Memory stability, leak rate, and bounded endurance tests

Definition

Memory stability under a repeated workload means that memory use stays within a bounded working range as the workload continues. The leak rate counts memory the program no longer needs and fails to release, per operation or per second. Two snapshots at comparable points reveal the net change in retained memory §11.37. Finding the cause of that change requires evidence about which allocations remain and why. A bounded endurance test, or soak test, repeats the workload for a stated number of operations or a stated time while recording resource use.

Where it fits

Main entry: Memory leak (§4.8).

After warmup §11.27, a cache might hold 1,024 bytes after one operation and 1,088 after the next, then return to 1,024. That variation is consistent with stability. The memory leak entry defines the fault itself: memory that a program has allocated, meaning reserved for its own use, no longer needs and never releases. This entry concerns the evidence about leaks that a measurement over time gives, usually a plot of memory in use against time or against the number of operations performed. A flat line on such a plot says one precise thing: the quantity that was counted, at the moments it was sampled, did not change by more than the plot can show. A flat line can still hide a leak in five ways.

Resources other than memory leak in the same way. File and network handles (numbers that identify an open file or connection), timers, queue entries and connections are each acquired and must each be released.

Estimate the net retention rate by taking two readings at the same point of the work cycle. In an invented run, the bytes allocated are 1000 before a batch of 100 operations and 1200 after it. The growth is 1200−1000=2001200 - 1000 = 200 bytes, so the average rate over that interval is 200/100=2200 / 100 = 2 bytes per operation. With 4096 bytes free, a constant 2 bytes per operation would use up the free memory after 4096/2=20484096 / 2 = 2048 further operations. That number is an extrapolation: it assumes that the growth continues at the same rate. A cache that fills up to a fixed 200 bytes and then stops growing produces exactly the same first interval. Later intervals can distinguish these two hypothetical patterns. A constant leak of 2 bytes per operation keeps adding 200 bytes every 100 operations. A full cache adds nothing. An allocation trace can identify which objects account for the increase. Growth while a cache or a buffer fills belongs to warmup, the period before a system settles into its steady state, in which start-up effects have ended and each repetition does the same work. A leak can also occur once during startup or only in an error path. State which phase and operation the reported rate covers.

Two choices make readings comparable. First, take every reading at the same point of the cycle, after each operation has completely finished. Some systems free memory only after a delay: a released object is kept until every part of the system that could still be using it has finished with it, a condition called quiescence §14.34. A reading taken right after an operation is submitted counts memory that is about to be freed, and that memory looks like growth. Second, record peak memory, the largest amount in use at any moment, separately from retained memory. A stable retained level says nothing about the peak, and the peak decides whether an operation fits.

Stack use is measured differently. The high-water mark, the deepest stack use observed, is usually found by filling each thread’s stack with a known byte pattern at start-up and later checking how much of the pattern was overwritten. It covers only the call paths that ran. A static bound, computed by analyzing every possible call path, also covers the paths that did not run.

A bounded endurance test of NN operations supports a bounded statement. Let the counter report memory in steps of rr bytes. Two equal readings taken NN operations apart mean that the true growth between them was less than rr bytes, so the average leak rate was below r/Nr / N bytes per operation, for the counted pool and the paths exercised. With r=1024r = 1024 and N=100N = 100, that limit is 1024/100=10.241024 / 100 = 10.24 bytes per operation; with N=10,000N = 10{,}000 it is about 0.1 byte per operation. A claim about a device’s whole service life, which may run millions of operations, needs either a test of that length or an argument about the code, such as a check that every allocation is released on every path.

Limits and common mistakes

A flat plot supports the statement “no growth detected in the counted pools, over this many operations, at this resolution”. It does not support “leak-free”.

A high-water mark is a lower bound on the worst-case stack use, not an upper bound: the worst case is at least as deep as the deepest use observed, and possibly deeper.

Restarts hide accumulation. A device that restarts every hour, for example because its watchdog timer §6.16 (a timer that restarts a device that has stopped responding) fires, never shows growth that would take a day to use up its memory. Count the restarts during the run, and compute growth only from readings within one run.

A stable retained level does not guarantee that allocations keep succeeding. Heap fragmentation §4.9, free memory broken into pieces too small for a request, can make an allocation fail while the total amount free stays constant.

A rate computed from one interval and projected forward is a model, not a measurement.

Questions answered

What else must be monitored?

Besides one plot of total heap memory (memory handed out on request while the program runs) over time, a memory stability test must monitor every resource that the repeated work acquires and must release, read at comparable points of the work cycle:

The report should give the duration, the number of operations, the counters recorded and their resolution. Even then the result is bounded: it shows that no growth was detected in those resources, over those operations, at that resolution. It does not show that the program never leaks.

Related entries: Peak versus retained memory (§11.37), Heap fragmentation (§4.9), Grace period (§9.21), Warmup and steady state (§11.27), Quiescent point (§14.34).