Calculate recommended heap size for a process based on workload and object allocation rate.
The recommended heap size must exceed the peak live object set by enough margin to accommodate allocator fragmentation (wasted space between allocated blocks) and garbage collector or allocator headroom (space needed for concurrent allocation during GC pauses or to avoid frequent compaction). A common rule of thumb is heap = peak_live × fragmentation_factor / (1 - headroom_fraction), targeting 2-3x the peak live set.
Recommended heap
heap = (peak_live × fragmentation_factor) / (1 - headroom%)
For glibc malloc under typical workloads, fragmentation is usually 1.2-1.5x. For languages with GC (Java, Go), the GC compacts the heap periodically so fragmentation is lower, but you need more headroom for GC working space.
Frequent garbage collections (GC thrashing) in managed languages, or failed allocations (OOM) in C/C++. The process spends increasing time in memory management rather than useful work, severely degrading throughput.