Counting Temporary Space by Lifetimes
A peak-space estimate counts the objects alive at once. Reusing temporary storage can reduce that peak while leaving total work similar.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Compare two traces
A program creates one buffer per row and releases it before reading the next row. Its peak extra storage includes one buffer at a time. Saving all buffers for a final display makes retention proportional to row count. Collection sizes and retained memory explain what those rows reference.
Account for nested calls
If a recursive call allocates a buffer and waits while another call allocates one, both are live. Use a recursive stack trace to identify the point of highest simultaneous retention.[1]
Compute the peak of simultaneous allocations
A pipeline allocates buffers of size four and seven during decoding, then releases both before allocating a sorting buffer of size nine. The peak temporary storage across these phases is eleven, not twenty. If the seven-unit buffer is retained for sorting, the peak grows to sixteen. See collection memory.
Decode overlap: 4 + 7 = 11
Sort alone: 9
Peak without retention: max(11,9) = 11
Sort with retained input: 7 + 9 = 16
Verify the lifetime assumptions in the implementation
A local variable can keep a buffer reachable after its last intended use. A recursive routine might also retain such buffers in suspended frames. Measure peak memory separately from the total number of bytes allocated across repeated calls.