Definition
A table of heap peaks, observed stack use and static bounds is a table of three measures of memory use. A heap peak is the largest amount of a heap in use at one moment; a heap is a memory region that a program allocates from. Observed stack use is the deepest a thread’s stack, which holds its function calls’ local data, was seen to grow. Both come from tested runs. A static bound is a limit computed by analysis without running the program, covering every execution the analysis includes.
Where it fits
Main entry: Peak versus retained memory (§11.37).
The peak memory entry defines a peak as the largest total in use at a single moment. Suppose two parts of a program, A and B, allocate from the same heap. Their joint peak is the largest value over time of the sum of their use, and that is not the sum of their separate peaks. Let and be the bytes that A and B have in use at time , and let mean the largest value over all times . Then because at every moment is at most its own maximum and is at most its own maximum. Equality holds exactly when some moment has both parts at their maxima at once.
In an invented run, at time 1 part A uses 4 KiB and part B uses 0 KiB; at time 2, A uses 0 KiB and B uses 6 KiB. The totals are KiB and KiB, so the joint peak is 6 KiB. The sum of the separate peaks is KiB, 4 KiB more than any moment needed. The sum is a safe overestimate of the joint peak, but nobody observed it. It is a valid upper bound only when each separate peak covers every execution that matters, not only the runs that were tested, and when no byte is counted in both parts.
Some systems give each part its own heap: a separate region with a size fixed when the program is built. Memory cannot then move between the heaps, so two different questions arise. Each heap’s peak must fit within that heap’s own size, and the RAM cost is the sum of the heap sizes. Adding peaks across separate heaps answers neither question. Before combining any numbers in such a table, check whether its heaps are one shared region or several separate ones.
Observed stack use is usually measured with a watermark, also called a high-water mark. The stack is filled with a known byte pattern before the run; afterwards, the deepest byte that no longer holds the pattern marks the deepest point the stack reached. A watermark covers only the call paths and interrupt §6.11 arrivals that happened in the tested runs. A static stack bound is computed from the call graph, the record of which function can call which. The bound is the largest total size of any chain of stack frames (a frame is the space one call reserves on the stack), plus the space that interrupts can add. On many processors, an interrupt saves registers on the stack in use when it arrives, and the function that responds to it may also run on that stack. The bound is valid only if the analysis knows every target of every indirect call (a call through a function pointer, whose target is chosen at run time), the program has no recursion (a function calling itself, directly or through others) or a known limit on it, and the analysis counts every level of interrupt nesting.
In an invented thread, the deepest chain of calls has frames of 64, 128 and 96 bytes, which is bytes. An interrupt arriving at that depth can add 32 bytes, so the static bound is bytes. The tested runs leave a watermark of 240 bytes, and the stack is 512 bytes long. The observed margin is bytes; the margin that the bound guarantees is bytes. The bytes in between are depth that the tests never reached but some execution may reach.
All data in this entry’s examples are invented.
Limits and common mistakes
A peak read by sampling, for example every 10 ms, can miss a peak that lasts 1 ms. An allocator (the code that hands out heap blocks) that updates its own record of the maximum on every allocation does not miss it.
A heap’s size and the blocks allocated inside it are not separate costs. An 8 KiB heap whose peak use is 6 KiB costs 8 KiB of RAM, not KiB.
A peak below the heap’s size does not guarantee that the next allocation succeeds. When free memory is split into small pieces, which is called heap fragmentation §4.9, a request for 2 KiB can fail while 3 KiB is free in total.
Heap use that returns to its starting value after each tested operation is evidence that those operations, on the paths tested, left nothing allocated in the heaps that were counted. It does not show that no path has a memory leak §4.8, and it says nothing about memory outside the counted heaps.
A watermark records the deepest byte written, not the deepest byte reserved. Space that a function reserves on the stack but never writes still holds the pattern, so the watermark can read less than the depth the stack actually reached.
A stack bound for one function is not a bound for a thread. The thread’s bound adds the frames of every function on its deepest chain, the space for interrupts and, in operating systems that save a paused thread’s registers on its own stack, that saved state too. Exceeding the stack’s size is a stack overflow §4.10.
Questions answered
Why cannot we add separate heap maxima?
Separate heap maxima cannot be added to obtain the most memory in use at once, because each maximum may occur at a different moment, while the joint peak is the largest total at one single moment. If part A peaked at 4 KiB while part B used nothing, and B peaked at 6 KiB after A had released everything, the most ever in use at once was 6 KiB, while the sum of the maxima is KiB. In general, with and the bytes each part holds at time and the largest value over all times, , with equality only when some moment has both parts at their maxima.
The sum still has one legitimate use: it is a safe upper bound on the joint peak, provided that each maximum is itself a bound over every execution that matters and that no allocation is counted twice. It must then be called a bound, not an observed peak. If the parts allocate from separate heaps, each with its own fixed size, the sum answers no useful question, because each heap’s peak must fit within that heap’s own size.
Related entries: Static versus dynamic footprint (§11.38), Memory leak (§4.8), Stack overflow (§4.10), Heap fragmentation (§4.9), Static, stack, and heap storage (§4.11).