Memory and File Systems

3.18 GB of Programs on a 16 GB Laptop

A

In this chapter

We'll see how the OS shares RAM between hundreds of processes using memory management and virtual memory, and how a file system turns a disk into files and folders you can find.

12–14 min

The Problem in Real Life

Anna adds up the Memory column in Task Manager. Chrome alone uses about 4 GB. VS Code, the video call, Node.js and all the rest use another 14 GB. That's 18 GB — on a laptop with only 16 GB of RAM.

"That's impossible," she says. John smiles. "It would be — if every process used real RAM directly. They don't."

A

How can my programs use more memory than my laptop has?

Anna

"Real RAM" vs. the Memory Each Program Sees

Every process thinks it's alone

Each process sees its own private memory, starting from the same addresses — the OS keeps them apart.

RAM runs out

When RAM is full, the OS quietly moves some memory to the disk.

A disk is just blocks

Without a file system, a disk is billions of numbered blocks with no names and no folders.

Memory Management and File Systems

Two of the OS's biggest jobs are looking after memory (RAM) and storage (the disk). Let's take them one at a time.

  • Memory management — the OS decides which process gets which part of RAM, makes sure no process can read or change another's memory, and takes memory back when a process ends.
  • Virtual memory — instead of giving programs real RAM addresses, the OS gives every process its own pretend, private set of addresses, called its virtual address space. Behind the scenes, the OS (with help from the CPU) maps those virtual addresses to real places in RAM. Memory is handled in small fixed-size pieces called pages (usually 4 KB).
  • Why virtual memory is so useful — every process is protected, because it can only reach its own addresses. Programs are simpler, because each one can act like it has the whole memory to itself. And the OS can use more memory than physical RAM, by keeping pages that aren't being used right now on the disk, in a swap file (Linux) or page file (Windows). When a process needs one of those pages again, the OS swaps it back into RAM.
Table — What a file really is
PartExample for config.json
Nameconfig.json
Data (contents){ "port": 3000, "currency": "USD" }
Size1.2 KB
Created / changed3 Feb / today 10:41
Owner and permissionsowned by root, others may only read (next chapter)
Where its blocks aretracked by the file system, invisible to you
Table — Common file systems
File systemUsed onNote
NTFSWindowsSupports permissions and large files
APFSmacOS, iPhoneBuilt for SSDs
ext4Most Linux serversStable, the usual Linux default
FAT32 / exFATUSB drives, SD cardsWorks almost everywhere, few features

Virtual memory, simply

Process A's addresses

private, virtual

Process B's addresses

private, virtual

every memory access

The OS + CPU map pages

virtual address → real location

stored in

RAM

pages in use right now — fast

Page / swap file on disk

pages not used lately — slow

So Anna's 18 GB fits in 16 GB because not all of it is in RAM at the same moment — some of it is waiting in the page file on the SSD. It works, but remember Act 02's memory hierarchy: the SSD is far slower than RAM. That's exactly why a laptop with too little RAM gets slow when you open too much. Also, Task Manager's numbers include memory that processes share (like common library code), so adding them up overcounts a little.

Now the second job: storage. A disk itself only knows numbered blocks of bytes. A file system is how the OS organises those blocks into things people can use:

A file is a named collection of data — a document, a photo, a program — plus information about it, called metadata: its name, size, when it was created and changed, and who's allowed to open it. A directory (or folder) is a special file that lists other files and directories. Directories inside directories form a tree, starting from one top directory called the root (/ on Linux and Mac, C:\ on Windows). Act 06 explores this tree with the terminal.

Different operating systems use different file systems: NTFS on Windows, APFS on Mac, ext4 on most Linux servers. They all do the same basic job — keep track of which blocks belong to which file — with different features. This is also why a USB drive formatted on one system sometimes can't be written to on another.

Key Takeaway

The OS gives every process its own private virtual memory and maps it to real RAM in pages, moving unused pages to a swap or page file when RAM is full. A file system turns a disk's raw blocks into named files with metadata, organised into a tree of directories.

Why This Matters

"Out of memory" crashes, slow servers that are swapping, full disks and missing files are everyday production problems. In Act 10, Anna will chase a memory leak in BlueTicket's seat-hold service; in Act 20, a server's disk dies. Both start from this chapter's two ideas: the OS shares memory through virtual memory, and stores everything through a file system.

The OS shares memory and organises files. One question is still open from this morning: the file Anna couldn't save. The file system knew exactly who owns it — and it wasn't her.

Next