7 routes42 sections324 units

Unit 1.6 · about 3 h

235 / 324

Resident 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

  1. Library allocation versus OS mappings
    Operating 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.

  2. Allocator retention
    AlgoMaster 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

  3. VSZ, RSS, PSS, and What They Mean
    AlgoMaster 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

  4. Mapping-level memory accounting
    Linux 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_rollup for detailed current totals and VmSize for mapped virtual size. ru_maxrss and VmHWM are peaks and cannot show a current-memory decrease.

Reproduce closed-book

Build a C allocation probe with 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

  1. Does malloc success prove every page has physical backing already?

  2. 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?

  3. Why might freeing alternate chunks leave residency high?

  4. Can a stable peak RSS prove stable current memory after freeing?

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

Comes after

Resources for this unit

  • Operating Systems: Three Easy Pieces - selected foundations
    Remzi and Andrea Arpaci-Dusseauprimarytheory
    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
    AlgoMastersupportrevision
    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
    man7.org and strace projectsupportreference
    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
    MIT PDOSrequiredcourse
    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
    OSTEP authorsrequiredpractice
    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
    Nand2Tetrisoptionalrepair
    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
    Sergey Slotinrequiredtheory
    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
    MITsupportcourse
    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
    LLVM Projectsupportreference
    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
    AlgoMastersupportrevision
    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.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels