Why is virtual memory 2 GiB when RSS is 24 MiB?
Question
Four worker processes run on a host with 4 KiB pages. At startup, each worker maps a 2,048 MiB scratch buffer and loads the same 16 MiB shared library. It then writes 1 byte into every page of the first 8 MiB of its scratch buffer. Your alert sees VSZ above 2,048 MiB, while RSS shows 24 MiB.
- Why is
RSSonly 24 MiB? - How many minor page faults did writing those 8 MiB cause?
- Why does summing
RSSacross all 4 workers overstate real RAM use, and what metric fixes it?
Show hints (3)
- "RESERVED VERSUS RESIDENT"
Virtual memory reserves address ranges in page tables. When does the kernel actually assign a physical RAM frame?
- "COUNT TOUCHED PAGES"
Convert 8 MiB to KiB, then divide by the 4 KiB page size. Writing 1 byte in a page still faults the whole page.
- "SHARED LIBRARY MATH"
Each worker counts the full 16 MiB library inside its own
RSS. How many copies sit in physical RAM?
Show checked explanation
Because of demand paging. Mapping 2,048 MiB reserves virtual addresses (VSZ) without backing them in RAM until you touch them. Writing the first 8 MiB triggers 2,048 minor page faults, adding 8 MiB of private RAM plus the 16 MiB shared library for 24 MiB RSS. Summing RSS counts the shared library four times; use PSS instead.
PAGE_KIB = 4 def memory_report(vsz_mib, touched_mib, shared_mib, workers): faults = (touched_mib * 1024) // PAGE_KIB rss_mib = touched_mib + shared_mib pss_mib = touched_mib + shared_mib / workers actual_ram_mib = workers * touched_mib + shared_mib return vsz_mib + shared_mib, faults, rss_mib, pss_mib, actual_ram_mib vsz, faults, rss, pss, ram = memory_report(2048, 8, 16, 4) print(f"VSZ={vsz}MiB faults={faults} RSS={rss}MiB PSS={pss:.0f}MiB") print(f"sum_RSS={rss * 4}MiB actual_RAM={ram}MiB")
VSZ=2064MiB faults=2048 RSS=24MiB PSS=12MiB sum_RSS=96MiB actual_RAM=48MiB
Ran the memory math on CPython 3.12.10 (Windows x64) and checked lazy virtual allocation working-set growth via Win32 VirtualAlloc. Checked by an editor other than the author.
Walkthrough
The OS allocates physical RAM lazily, one page at a time. Calling mmap (or VirtualAlloc on Windows) for 2,048 MiB only records a virtual address range. No physical frames move until your code touches an address inside that range.
Each time the worker writes 1 byte into a new 4 KiB page, the CPU raises a minor page fault. The kernel grabs a zeroed physical frame, updates the page table, and resumes the instruction. Touching 8 MiB (8,192 KiB) across 4 KiB pages causes 8,192 / 4 = 2,048 minor page faults and commits 8 MiB of physical RAM. The remaining 2,040 MiB stays virtual only.
| metric | per worker | sum across 4 workers | what it measures |
|---|---|---|---|
VSZ (VmSize) | 2,064 MiB | 8,256 MiB | Total mapped virtual address space |
RSS (VmRSS) | 24 MiB | 96 MiB | Private RAM (8) + full shared library (16) |
PSS | 12 MiB | 48 MiB | Private RAM (8) + 1/4 share of library (4) |
All 4 workers map the same 16 MiB shared library in physical RAM. RSS charges the full 16 MiB to every worker, so 4 * 24 = 96 MiB overstates true RAM usage by 48 MiB. Proportional Set Size (PSS) from /proc/<pid>/smaps splits shared pages by the number of processes mapping them: 8 + 16 / 4 = 12 MiB per worker, summing cleanly to the true 48 MiB of RAM.
Common mistakes
- Paging on-call for high
VSZ. Largemmapfiles, guard pages, and runtime arenas inflateVSZwithout using physical RAM. - Adding up
RSSacross processes to estimate host memory usage. Shared libraries and memory-mapped files get counted once per process. - Assuming writing 1 byte allocates only 1 byte of RAM. Hardware page tables map memory in full pages, typically 4 KiB each.
- Confusing minor and major page faults. A minor fault maps an in-memory or zero page; a major fault blocks to read data from disk.
Follow-up questions
- "If a worker later fills the entire 2,048 MiB buffer on a 1 GiB container, when does it fail?" Not at
mmaptime under Linux default overcommit. It fails later during page faults, when resident memory hits the cgroup limit and the OOM killer terminates the process. - "Can
RSSdrop even if your application never callsfree()?" Yes. Under memory pressure the kernel can evict clean file-backed pages or move anonymous pages to swap. Virtual mappings (VSZ) stay the same whileRSSfalls. - "Where on Linux do you read exact
RSSandPSS?" Read/proc/<pid>/smaps_rollupor/proc/<pid>/smaps. The kernel documentation notes thatVmRSSin/proc/<pid>/statusis a fast approximation, whereassmapswalks the page tables directly.
What a strong answer covers
Explains demand paging with the exact 2,048-fault math, distinguishes virtual reservations from resident frames, and uses PSS to fix RSS double-counting of shared memory.
Open in interactive practice deck →