Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 55 ยท Operating Systems

Memory Management

Understand how an operating system manages memory: address spaces, paging, virtual memory, page faults and swapping, plus why apps leak memory and crash.

Every running program needs memory for its code and data, and a computer runs many programs at once. If they all wrote wherever they liked, one bug would overwrite another program, or the operating system itself. Memory management is the job of giving each process the memory it needs, keeping processes apart, and making a limited amount of RAM stretch as far as possible.

The trick that makes this work is virtual memory: each process sees its own tidy, private range of addresses, and the OS (with help from the CPU) quietly maps those addresses onto real RAM, or onto disk when RAM runs short. This lesson builds on Memory: RAM, Cache and the Memory Hierarchy and Processes, Threads and Scheduling.

Address spaces: a private view

A process never works with physical RAM addresses. It uses virtual addresses, and each process has its own address space that starts at the same low numbers. Two programs can both store something at virtual address 5000 without clashing, because each 5000 points to a different place in RAM.

A hardware unit in the CPU, the memory management unit (MMU), translates every virtual address into a physical one on the fly, using tables the OS maintains. This gives three benefits:

  • Isolation. A process can only reach memory the OS mapped for it. A stray pointer causes a crash of that one process, not of the whole machine.
  • Simplicity. A programmer does not need to know where in RAM the program landed or what else is loaded.
  • Flexibility. The OS can move data around, share one copy of a library between many processes, or push rarely used data out to disk.

Paging

Translating byte by byte would need an enormous table, so the OS divides memory into fixed-size blocks. Virtual memory is cut into pages and physical RAM into frames of the same size; 4 KiB is the usual page size. A page table records, for each page of a process, which frame holds it.

A virtual address is therefore split in two: the page number (which page) and the offset (which byte inside that page). Translation replaces the page number by a frame number and keeps the offset.

flowchart LR
  VA["Virtual address<br/>page 2, offset 0xA5C"] --> PT["Page table<br/>page 2 -> frame 7"]
  PT --> PA["Physical address<br/>frame 7, offset 0xA5C"]

Translation changes the page number to a frame number; the offset stays the same.

Because pages can be placed in any free frames, memory does not need to be contiguous, and the OS avoids the wasteful gaps (fragmentation) that appear when variable-sized chunks are allocated and freed. The stand-in below performs a translation with a tiny page table.

JS Translate virtual addresses with a page table

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The third outcome, a page that is valid but marked "not in RAM", leads to the most interesting part of virtual memory.

Virtual memory and page faults

A machine usually has less RAM than all processes would like to use. Virtual memory lets the total size of all address spaces exceed RAM: pages that are not currently needed live on disk, in a swap file or swap partition (Windows calls it the page file), and only the active pages occupy frames.

When a process touches a page that is not in RAM, the MMU raises a page fault. This is not an error. The OS pauses the process, finds the page on disk, loads it into a free frame, updates the page table and resumes the process, which never notices anything except a delay. Reading from an SSD takes microseconds to hundreds of microseconds, while RAM takes nanoseconds, so a page fault costs thousands of times more than a normal access. A fault caused by an address that is not mapped at all is different: it ends the program with a "segmentation fault" or "access violation".

If RAM is full, the OS must choose a victim page to evict (writing it to disk first if it was changed) to make room. The rule for choosing is a page replacement algorithm:

  • FIFO evicts the page that arrived first, which is simple but can throw away a page that is still heavily used.
  • LRU (least recently used) evicts the page unused for longest, betting on locality: recently used pages will probably be used again.
  • Optimal evicts the page whose next use is furthest in the future. It needs to know the future, so no real system can do it, but it gives a benchmark.

The simulator counts page faults for a short sequence of page requests with different numbers of frames.

JS Page replacement simulator

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Look at the FIFO row: with more frames it produced more faults. This surprising result is called Belady's anomaly, and it happens with FIFO but not with LRU or the optimal policy, which never get worse when given more memory. It is a good reminder that a policy should follow how programs actually behave, not just arrival order.

When the OS is so short of RAM that it spends most of its time moving pages in and out instead of running programs, the machine thrashes: everything crawls and the disk light stays on. The fix is more RAM, fewer running programs, or a smaller working set.

Stack, heap and leaks

Inside its address space, a process uses memory in two main ways. The stack holds function calls and local variables; it grows when a function is called and shrinks when it returns, automatically. The heap holds data whose lifetime is not tied to one function, such as a list that grows while the program runs. Heap memory must be released when no longer needed, either by the programmer (as in C) or by a garbage collector (as in Java, Python or JavaScript).

A memory leak is memory that a program no longer needs but never releases. Each leak is small, but a program that runs for days, such as a browser with many tabs or a server, keeps accumulating them, and its memory use climbs slowly. In garbage-collected languages a leak usually means the program still holds a reference to old data, for example a cache that is never emptied.

From a user's perspective, you notice leaks as an application that gets slower and bigger the longer it runs, and goes back to normal after a restart. Quitting the program is a real fix because when a process ends, the OS reclaims all of its memory, leaked or not.

JS A leaking cache versus a bounded cache

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Running out of memory

What happens when memory truly runs out, with RAM full and swap full too? The OS has no frame to give. Depending on the system, the program asking for memory gets an error and may exit ("out of memory"), a browser shows its "Aw, snap" tab crash, or the OS picks a process to terminate to save the rest (Linux has an "OOM killer" for this). Mobile operating systems are stricter: they close background apps when memory is needed, which is why a phone app may restart from scratch after you return to it.

Practical advice follows from this: close programs you are not using, restart long-running applications that keep growing, keep enough RAM for your workload, and remember that a full disk can also hurt because there is no space left for swap.

What is real here and what is a stand-in? Paging, page tables, page faults, swapping and the replacement policies are real. The simulators use JavaScript arrays and Maps to model them. A real MMU translates in hardware with multi-level tables and a small cache of recent translations (the TLB), and real systems use approximations of LRU because tracking exact recency would be too slow.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the MMU do?

    The memory management unit uses the page tables to map each process's virtual addresses to RAM.
  2. 2. A virtual address splits into which two parts?

    The page number selects the page-table entry; the offset is the byte within the page and is copied unchanged.
  3. 3. What happens on a page fault for a valid page that is on disk?

    A page fault is a normal event for swapped-out pages. It is just slow, because disk is far slower than RAM.
  4. 4. What is thrashing?

    When the working sets of the running programs do not fit in RAM, the pages are constantly evicted and reloaded.
  5. 5. Which description best fits a memory leak?

    Leaks accumulate during a long run. Ending the process returns all its memory to the OS.

Summary

  • Each process has a private virtual address space; the MMU translates its addresses to physical RAM using page tables.
  • Memory is split into fixed-size pages (frames in RAM); an address is a page number plus an offset.
  • Virtual memory lets processes use more than the installed RAM by keeping inactive pages in swap on disk.
  • A page fault loads a missing page; page replacement (FIFO, LRU, optimal) chooses what to evict, and thrashing means too much swapping.
  • The stack holds function calls, the heap holds longer-lived data; leaks are heap memory that is never released.
  • When memory runs out, programs fail or the OS ends processes; restarting leaky applications and adding RAM help.

Next, see how data is organized on the disk: File Systems.