Unit 1.6 · about 3 h
235 / 324Resident size, virtual size and allocator retention
Assume Programming's profiling-benchmarking unit; Python object-allocation profiles are background, not an RSS oracle. This unit follows native allocation into kernel mappings and distinguishes a leak from reusable retained capacity.
- measure
You can now
- Measure live requested bytes, virtual size, RSS and PSS as different quantities.
- Diagnose retained allocator capacity using staged allocate/free/reuse observations.
- Separate current residency, peak residency and cgroup-wide accounting.
Covers
- Resident size, virtual size, allocator behavior and retention
Study, in this order
4 steps
- Library allocation versus OS mappingsOperating Systems: Three Easy Pieces - selected foundations
Chapter 14, Sections 14.1 to 14.3 and 14.5
Depth · Assume ownership and invalid-free rules from Programming. Focus on why malloc/free are library calls and brk/mmap are kernel interfaces.
- Allocator retentionAlgoMaster Operating Systems - selected observation route
malloc() as a Library Function; Arenas and Chunks; What free() Does; Coalescing Adjacent Free Chunks; Obtaining Memory from the Operating System; Why free() May Not Reduce Process Memory; Multiple Arenas and Thread Caches; Observing malloc() with strace
- VSZ, RSS, PSS, and What They MeanAlgoMaster Operating Systems - selected observation route
VSZ: Virtual Size through Comparing the Four Metrics; Reading Memory Metrics on Linux; A Hands-On Allocation Experiment; Why free() May Not Reduce RSS; Measurement Pitfalls
- Mapping-level memory accountingLinux proc and strace references
DESCRIPTION: Size, Rss, Pss, private/shared fields, KernelPageSize, MMUPageSize and AnonHugePages
Focus on
- Track requested live payload independently of allocator metadata, rounding and free chunks. The kernel accounts mappings and pages, not each application allocation.
- RSS counts shared pages fully in each process; PSS apportions them. Neither is identical to the hot working set or a cgroup's charged memory.
- Free returns an object to the allocator. Reuse can consume retained chunks without reducing RSS first; live neighbors can prevent returning a whole arena region.
- Use
smaps_rollupfor detailed current totals andVmSizefor mapped virtual size.ru_maxrssandVmHWMare peaks and cannot show a current-memory decrease.
Reproduce closed-book
gcc -O2 -Wall -Wextra. At baseline and after each stage, read /proc/self/status VmSize and /proc/self/smaps_rollup Rss, Pss and private fields. Allocate 8192 blocks of 8192 bytes, checking every malloc; write through a volatile unsigned-char pointer once per sysconf(_SC_PAGESIZE) and at the last byte. Free even-index blocks while keeping odd ones live, reallocate those same-size blocks and touch them, then free all blocks. Print live requested payload at each stage: 0, 64 MiB, 32 MiB, 64 MiB, 0. Run three fresh processes pinned with taskset -c 2 and record CPU, kernel, GCC and glibc versions, raw snapshots and which pages were touched. Explain the observed retention or release; do not require RSS to equal payload or return exactly to baseline. A diagnostic strace -e trace=brk,mmap,munmap run can locate kernel mapping changes but is not a timing run.Traps
- Calling retained free chunks a leak from one RSS snapshot. Show live ownership and bounded reuse before diagnosing growth.
- Reading an uninitialized or freed block to force residency. Write while it is live and never dereference it after free.
- Adding worker RSS values as physical usage, or interpreting a peak as current memory.
Practice
The section mini-project covers this: staged live-byte and smaps accounting, repeated allocate/free/reuse cycles, one sparse mapping, and a diagnosis distinguishing retained capacity, shared pages and unbounded ownership growth.
Self-test
-
Does malloc success prove every page has physical backing already?
No. Address-space reservation and allocator bookkeeping can succeed before first-touch backing is supplied. Overcommit and later resource pressure can still cause failure. -
In a toy model, two processes each map 8 MiB private and the same 4 MiB shared resident pages. What are RSS and PSS per process?
RSS is 12 MiB each; PSS is \(8+4/2 = 10\) MiB each. Their represented physical footprint is 20 MiB, not the 24 MiB RSS sum. -
Why might freeing alternate chunks leave residency high?
Live chunks can pin the surrounding arena pages or prevent region release, and the allocator can retain free chunks. The exact behavior depends on allocator policy and layout. -
Can a stable peak RSS prove stable current memory after freeing?
No. Peaks do not fall. Compare current snapshots and ownership over repeated workload cycles.
Completion criterion
Stop when
Your staged probe reports all five live-payload totals and current snapshots, and you explain retained or released pages without equating free, unmap, current RSS and peak RSS.
Mark passed, then open 1.7 Only mark this when you can do it without notes.Leads to
- 1.9 Namespaces, cgroups and container boundaries Diagnose memory limits using the cgroup's scope, not process RSS alone.
Comes after
- 1.5 Virtual memory, page tables, TLBs, page faults and mmapAccount for mappings, resident pages and retained allocator capacity.
- 1.9 Namespaces, cgroups and container boundariesReconcile cgroup charges with process-level residency before changing a memory budget.
Resources for this unit
- Operating Systems: Three Easy Pieces - selected foundations Read Chapters 4-7, 13-15, 18-21, 36, 39-40, 42 and 45. Work from process state to address translation, devices, files, journaling and integrity. For every chapter, draw the owned state and one failure boundary instead of collecting summaries.
- AlgoMaster Operating Systems - selected observation route After OSTEP, use the matching lessons as a second explanation and recall check: fork, exec, wait, and exit; Zombies and Orphans; Signals; Context Switching; The Process Address Space; Paging; The TLB: Hits, Misses, and Invalidation; Page Faults; Copy-on-Write; mmap and Memory-Mapped Files; How malloc Works; VSZ, RSS, PSS, and What They Mean; File Descriptors; The Page Cache and Write-Back; Durability and fsync; Crash Consistency; Linux Namespaces; Control Groups; PID 1 and Signal Handling; Container Resource Failures; and the Investigating lessons. Treat quizzes as recall checks and keep your own traces as evidence.
- Linux proc and strace references Use proc(5), getrusage(2), mmap(2), fsync(2), rename(2), wait(2) and signal(7) while tracing a parent-child pipe, page faults, file publication and a resource-limit failure. Record exact commands and raw observations.
Also in this section
- MIT 6.1810 - selected xv6 process and memory labs Use Fall 2025 Lectures 1, 3-6, 8 and 10-11. Complete the util lab, one syscall task, and the page-table inspection plus ugetpid task from pgtbl. Add cow only if copy-on-write remains unclear. Keep all work in your own course checkout and stop before the full xv6 sequence.
- OSTEP homework - process, paging, disk and integrity simulators Solve process-run.py, paging-linear-translate.py, paging-policy.py, disk.py, fsck.py and checksum.py for at least two seeds each before using the answer flag. Then reproduce one prediction using strace, /proc or a small local program.
- Nand2Tetris - Projects 1 and 4 only Use Project 1 for Boolean logic and Project 4 for machine-language execution only if the binary or instruction-level entry check fails. Do not take the full course as a second systems curriculum.
- Algorithmica - Algorithms for Modern Hardware Use the Instruction-Level Parallelism, SIMD Parallelism and RAM & CPU Caches chapters for measured pipeline, branch, vectorization, bandwidth, latency and cache-line effects. The figures are the author's Zen 2 measurements at a fixed 2 GHz: predict and measure your own.
- MIT 6.S894 Accelerated Computing - Throughput Processors Use Lecture 2 for the processor model behind this section: superscalar issue, out-of-order execution, speculation and SIMD's need for coherent control. The labs run on the course's remote machines and target AVX-512 and NVIDIA GPUs, so they belong to the GPU track.
- Auto-Vectorization in LLVM Use The Loop Vectorizer Usage and Diagnostics sections for the Clang remarks the pipelines unit asks for; the Features list is optional depth.
- AlgoMaster: hardware and container storage companions Use Memory Ordering, Barriers, and False Sharing (coherence and false-sharing headings only); Huge Pages, NUMA, and ASLR (NUMA headings only); Storage Hardware; RAID Fundamentals (comparison and backup boundaries); and Container File Systems. These fill hardware and image-layer gaps outside the pool's selected OS revision lessons.