# Memory Management and Virtual Memory

> Understand paging, segmentation, page tables, TLBs, virtual memory, page faults, page replacement algorithms and thrashing. Free notes, key concepts, resources...

Canonical: https://www.internhack.in/roadmaps/tracks/cs-fundamentals/memory-management
Last updated: 2026-11-28

Understand paging, segmentation, page tables, TLBs, virtual memory, page faults, page replacement algorithms and thrashing.

Intermediate · about 8 hours · difficulty 4/5

Learn first: [Processes and Threads](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/processes-and-threads)

Every process believes it has a large, private, contiguous memory. **Virtual memory** creates that illusion, and it is the densest OS topic in interviews.

### From physical to virtual

- Early schemes used **contiguous allocation** with fixed or variable partitions. They suffer **internal fragmentation** (waste inside a block) and **external fragmentation** (free memory in scattered holes). Know first-fit, best-fit and worst-fit.
- **Paging** splits virtual memory into fixed-size **pages** and physical memory into **frames** of the same size. A **page table** maps page numbers to frame numbers. No external fragmentation, small internal fragmentation.
- Address translation: split the virtual address into page number and offset; practise questions like "32-bit address, 4 KB pages, how many page table entries?" (2^20).
- The **TLB** caches recent translations; effective access time questions combine TLB hit ratio with memory access time.
- **Multi-level page tables** save memory for sparse address spaces. **Segmentation** divides memory by logical units (code, stack) and can be combined with paging.

### Virtual memory and demand paging

- Pages are loaded only when accessed. Accessing a page not in memory causes a **page fault**, handled by the OS reading it from disk.
- **Page replacement**: FIFO, **LRU**, **Optimal** (theoretical best, needs the future), and Clock (an LRU approximation). Practise counting faults for a reference string.
- **Belady's anomaly**: with FIFO, more frames can cause more faults. LRU and Optimal do not suffer from it.
- **Thrashing**: the system spends more time paging than running because the working sets do not fit. Fix with the working set model or by reducing multiprogramming.

### Also know

Stack vs heap, memory leaks, and why `malloc`/`new` are not free operations.

Good enough to move on: you can compute page faults for FIFO, LRU and Optimal on a reference string and explain what happens step by step on a TLB miss followed by a page fault.

## Key concepts

- **Contiguous allocation and fragmentation**
- **Paging and page tables**
- **Address translation numericals**
- **TLB and effective access time**
- **Segmentation**
- **Demand paging and page faults**
- **FIFO, LRU and Optimal replacement**
- **Belady's anomaly**
- **Thrashing and working sets**
- **Multi-level page tables**

## Mini project

Write a page replacement simulator that takes a reference string and frame count and reports faults for FIFO, LRU, Optimal and Clock, and use it to demonstrate Belady's anomaly with FIFO on the string 1,2,3,4,1,2,5,1,2,3,4,5.

## Check yourself

Walk through what happens, step by step, when a program reads an address whose page is not in the TLB and not in physical memory.

## Free resources

- [OSTEP: Virtualization of memory chapters](https://pages.cs.wisc.edu/~remzi/OSTEP/) (OSTEP)
- [GeeksforGeeks: Page Replacement Algorithms](https://www.geeksforgeeks.org/page-replacement-algorithms-in-operating-systems/) (GeeksforGeeks)

Previous: [Deadlocks](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/deadlocks) · Next: [File Systems, Storage and I/O](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/file-systems-and-io)

## More in Operating Systems

- [Processes and Threads](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/processes-and-threads)
- [CPU Scheduling](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/cpu-scheduling)
- [Concurrency and Synchronization](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/concurrency-synchronization)
- [Deadlocks](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/deadlocks)
- [Memory Management and Virtual Memory](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/memory-management)
- [File Systems, Storage and I/O](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/file-systems-and-io)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [CS Fundamentals and Placement Prep Roadmap](https://www.internhack.in/roadmaps/tracks/cs-fundamentals)
