Chapter 32

The Page Is the Atom

When you allocate an array in C or JavaScript, you get a pointer: 0x1000.

You assume that 0x1000 is a real location in a memory chip.

It is not. It is a lie directed at your face by the OS.

The Address Formula

The CPU speaks in Virtual Addresses (e.g., 0x12345678).

The MMU splits this number into two parts:

Crucial Insight: The OS translates the Page Number to a Physical Frame. The Offset stays the same. The "Atom" of memory management is the Page. You cannot translate "half a page."

The Virtual Lie

Every program believes it owns the entire memory (e.g., all 4GB).

In reality, the OS hands out Pages (4KB chunks). It maps "Virtual Page 1" to "Physical Frame 99".

When you touch a page that hasn't been mapped yet, the CPU halts. It screams for the OS. This is a Page Fault.

Minor Fault: The page is in RAM (maybe shared by another process), just not mapped to you. The OS updates the table. Fast-ish.

Major Fault: The page is NOT in RAM. It must be fetched from Disk. The OS puts your thread to sleep for milliseconds. This is the killer.

Physics Lens: Virtual = Infinite. Physical = Scarce. Fault = Latency Cliff.
Experiment:
1. Locality: Click x00 then x50. They are in the same Page. TLB HIT. Fast.
2. Crossing the Boundary: Click x100. New Page. FAULT. The OS works.
3. Thrashing: Click x200, x300, x400. You run out of Frames. Evictions begin.

Granularity: The 4KB Atom

Here is the trap: Memory is not manipulated in bytes. It is manipulated in Pages.

If you read 1 byte from a new page, the system must fetch all 4096 bytes.

If you write 1 byte to a mapped file, the system marks the entire 4KB page as "Dirty".

This is why random access to large datasets is deadly. You are not fetching bytes; you are loading thousands of heavy 4KB crates, taking one item, and throwing the rest away.

Hypotheses
Why is it 4KB?

It's a compromise. If pages were smaller (e.g., 64 bytes), the Page Table would be massive (storing billions of entries). If pages were larger (e.g., 1MB), we would waste memory (Fragmentation) if you only needed 1KB. 4KB was chosen in the 80s and stuck.

What is a Segfault?

A Segmentation Fault occurs when you touch a Virtual Address that the OS has deciding is invalid (not mapped to anything legal). The OS doesn't fix it; it kills you.

The RAM is full.

In our lab, we evicted pages to make room.

But where did the evicted data go? If we need it again, do we just recreate it? No. We must save it to the Disk.

And that is where the computer dies.