Terminology (6)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Page
- A unit of OS virtual-memory mapping. Its size depends on the platform and mapping; the teaching model fixes it at 4096 bytes.
- Reserved
- Address space the process has set up, for example with mmap, whether or not physical memory backs it yet.
- Resident (RSS)
- Memory currently resident for this process’s mappings, including shared pages. RSS is not a count of live objects or exclusively owned physical bytes.
- Allocator
- The library code (malloc, the Go runtime, V8, Rust’s global allocator) that cuts pages into the small blocks your code requests.
- Size class
- A fixed block size an allocator uses for requests up to that size, so equal blocks can share a page.
- mmap
- The system call that maps new pages into the address space; allocators use it to obtain memory.
Three different memory accounts
An allocator supplies storage for values; the operating system manages virtual-memory mappings in units called pages. Live bytes describe storage still used by values. Reserved virtual space describes mappings the process has arranged. Resident memory (RSS) counts pages currently resident for its mappings, including shared pages; it is not a count of live objects or exclusively owned bytes.
A common small-object allocator rounds requests into size classes and serves slots from memory it holds; larger requests can follow another path. The OS supplies mappings through facilities such as mmap. Fresh anonymous zero memory can be demand-paged, but prefaulting, allocator reuse, page sizes and platform policies affect when backing pages are needed.
01From pages to values
Three layers between your value and physical memory
A map-tile service reserves room for a large tile index before traffic arrives. Later it evicts most cached entries, but its resident-memory dashboard barely falls. Before changing the cache limit, distinguish the bytes the cache still uses, the space its allocator holds and the pages the OS currently keeps resident.
For the tile-cache investigation, keep each layer’s counters separate while probing startup allocation and later eviction.
| Layer | Unit | Where you see it |
|---|---|---|
| Your code | Values and objects | Live bytes (heap profilers, runtime stats) |
| Allocator | Size-class slots in pages it holds | Allocator or runtime statistics |
| Operating system | Mappings and currently resident pages | VmRSS/smaps; container totals use separate accounting |
02Resident when touched
Allocating is not using
First isolate the sparse index’s startup allocation. The probe samples process VmRSS, creates a zero-filled 64 MiB buffer, then writes at a fixed 4096-byte stride and samples again. The stride is authored, not a measurement of the OS page size. VmRSS is approximate and includes unrelated process activity, so deltas can be noisy or negative; the Rust helper uses signed arithmetic for that reason.
import { readFileSync } from 'node:fs';
/** Resident memory of this process in MiB, from Linux's /proc/self/status. */
function residentMiB(): number {
const kib = Number(/VmRSS:\s+(\d+)/.exec(readFileSync('/proc/self/status', 'utf8'))![1]);
return Math.round(kib / 1024);
}
const PAGE = 4096;
const SIZE = 64 * 1024 * 1024;
const start = residentMiB();
const buffer = new Uint8Array(SIZE);
const allocated = residentMiB();
for (let offset = 0; offset < SIZE; offset += PAGE) buffer[offset] = 1;
const touched = residentMiB();
console.log(`after allocating 64 MiB: +${allocated - start} MiB resident`);
console.log(`after writing one byte per page: +${touched - start} MiB resident`);
npx tsc --noEmit --strict main.ts && node main.ts
03Giving memory back
Freed values are not freed pages
After the busy period, the service evicts cached entries. Storage can become reusable inside an allocator without its mappings disappearing. A non-moving allocator may keep a partly occupied page because the remaining value still needs its bytes. Empty pages can be retained, unmapped or have their backing discarded while the virtual mapping remains; RSS can also change through OS reclaim. The lab will choose one explicit release policy rather than infer all of this from one counter.
04Practice
Model an allocator’s pages
Build a diagnostic model for the tile cache: requested record bytes round into slots, writes make a model page resident, and the final free on a page releases that page. It uses 4096-byte pages and fixed classes, with allocation names unique while live and positive decimal integer requests. Oversized requests report too large. Its resident flag changes only by these commands; empty-page release is an authored policy, not a promise that a real allocator always returns every empty page.
A page allocator model
A small allocator model reserves pages and cuts them into size-class slots. Pick the size class, count which pages are resident, then decide when a page can go back to the operating system.
Check this idea later
Sources
- Linux: malloc and glibc allocation behavior
- C++ draft: vector constructors
- Rust: global allocator selection
- Linux: madvise and resident backing
- Linux man page: mmap(2)
- Linux man page: proc_pid_status(5), VmRSS
- glibc malloc internals
- Go: runtime/debug.FreeOSMemory
- Rust: std::alloc::GlobalAlloc
- V8: trash talk, the Orinoco garbage collector
In the frontend Label process RSS separately from managed heap and buffer storage. Optional
The tile-cache dashboard needs to distinguish cache eviction from process-memory release. Its server can report Node memory counters, but a falling heap value and flat RSS answer different questions. These measurements describe one server process, not the browser or the whole deployment.
import { memoryUsage } from 'node:process';
export const runtime = 'nodejs';
export function GET() {
const { rss, heapUsed, arrayBuffers } = memoryUsage();
return Response.json({ rss, heapUsed, arrayBuffers });
}
The Node App Router handler returns byte counts. heapUsed is used V8 heap, not an exact count of reachable tile entries; arrayBuffers includes ArrayBuffer backing storage, while RSS covers the wider resident process. These categories overlap and must not be added as independent totals. A response after eviction does not guarantee a collection or allocator release.
Sources: Node.js: process.memoryUsage,Next.js: Route Handlers,SvelteKit: server routes.
Sign in to keep your read marks on every device. Sign in
Was this helpful?