Skip to content
GATEiit

GATE CSE · Operating System

Memory management and virtual memory

Contiguous allocation and fragmentation, paging and page-table size, multilevel paging, TLB access time, segmentation, demand paging with effective access time, page replacement (FIFO, LRU, optimal), Belady's anomaly and thrashing.

Before you start

This topic comes before: Memory interfacing and hierarchy: performance and cache memory mapping

Concept notes

1. Contiguous allocation and fragmentation

The CPU generates logical (virtual) addresses; the MMU maps them to physical addresses. In the simplest scheme a relocation (base) register is added to each address and a limit register checks that it is in range.

With variable partitions, free memory is a set of holes. To place a process of size s:

Policy Picks Note
First fit first hole ≥s fast; usually good
Next fit like first fit, from where the last search stopped spreads allocations
Best fit smallest hole ≥s leaves tiny unusable holes; searches all
Worst fit largest hole leaves big holes; often worst in practice

Fragmentation:

  • External: enough total free memory, but not in one piece. Happens with variable partitions and segmentation. Fixed by compaction (needs dynamic relocation) or by paging.
  • Internal: space wasted inside an allocated block, because blocks come in fixed sizes. Happens with fixed partitions and paging (on average half a page per process).

Segmentation divides a program into logical units (code, stack, data). A logical address is (segment, offset); the segment table holds base and limit, and an offset ≥ limit causes a trap. Segments vary in size, so segmentation has external fragmentation but no internal fragmentation.

2. Paging, page tables and the TLB

Paging splits logical memory into fixed-size pages and physical memory into frames of the same size. No external fragmentation.

With page size 2d bytes, a logical address of L bits splits into page number (L−d bits) and offset (d bits). The page table maps page p to frame f, and the physical address is (f,d).

  • Entries in the page table =2L−d.
  • Page-table size = entries × PTE size.
  • Frame-number bits = physical address bits −d.

Multilevel paging: a big page table is itself paged. If a page holds 2d/e entries (e = PTE size), each level resolves log⁡2(2d/e) bits, and the number of levels is ⌈L−dlog⁡2(2d/e)⌉. Each level adds a memory access on a TLB miss.

Inverted page table: one entry per frame, shared by all processes; small, but lookup needs a search or hash.

The TLB is a small associative cache of recent page→frame mappings. With TLB time t, memory time m, hit ratio h and one-level paging:

EMAT=h(t+m)+(1−h)(t+2m)

For k-level paging, a miss costs t+(k+1)m. (Some questions assume the TLB is searched in parallel, making the hit cost just m; read the wording.)

CPUpdlogicalTLBpage → framefdphysicalMemoryPage tablehitmiss
Address translation: the TLB is checked first; on a miss the page table gives the frame

3. Demand paging and effective access time

With virtual memory, a process can be larger than physical memory. Demand paging loads a page only when it is first referenced. Each PTE has a valid bit; touching an invalid page causes a page fault (a trap):

  1. The OS checks that the reference is legal.
  2. It finds a free frame, or picks a victim (writing it back first if its dirty bit is set).
  3. It reads the page from disk, updates the page table, and restarts the instruction.

With page-fault rate p, memory access time m and page-fault service time s:

EAT=(1−p) m+p s

Because s is in milliseconds and m in nanoseconds, even p=0.001 makes EAT about a hundred times m. Keep units consistent (1 ms =106 ns). If a dirty victim must be written back, the service time includes that extra disk write.

Thrashing: when processes have too few frames, they fault constantly and spend more time paging than computing; CPU utilisation falls as the degree of multiprogramming rises. The fix is to give each process its working set (the pages used in the last Δ references) or to reduce the number of processes. Page-fault frequency control adds frames when a process faults too often and takes them away when it rarely faults.

4. Page replacement algorithms

When no frame is free, a victim page is chosen.

Algorithm Replaces Note
FIFO the page loaded earliest simple; suffers Belady's anomaly
Optimal (OPT) the page whose next use is farthest in the future fewest faults possible; needs the future, used as a benchmark
LRU the page unused for the longest time good in practice; needs counters or a stack
Clock (second chance) FIFO, but skips pages with reference bit 1 (clearing it) approximation of LRU

Belady's anomaly: with FIFO, giving a process more frames can increase the number of faults. For the string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5, FIFO gives 9 faults with 3 frames and 10 with 4.

OPT and LRU are stack algorithms (the pages held with n frames are always a subset of those held with n+1), so they never show Belady's anomaly.

How to trace: write the frames after each reference and mark each fault. The first references to distinct pages are always faults (compulsory misses). For OPT, on a tie between pages never used again, any choice gives the same count.

Formulas to remember

Page-table entriesL-bit logical address, page size 2d bytes
2L−d
Page-table sizee = bytes per PTE
2L−d×e
Levels of pagingWhen each page table must fit in one page
⌈L−dlog⁡2(2d/e)⌉
EMAT with TLB, one-level paging
h(t+m)+(1−h)(t+2m)
TLB miss with k-level paging
t+(k+1) m
Effective access time with page faults
(1−p) m+p s
Average internal fragmentation (paging)
12 page per process

Common mistakes

  • Forgetting the TLB lookup time on a miss, or the extra memory access for each page-table level.
  • Mixing milliseconds and nanoseconds in the effective access time.
  • Using physical address bits to count page-table entries; entries depend on the logical address and page size.
  • Saying paging has external fragmentation; it has internal fragmentation only.
  • Assuming more frames always means fewer faults; FIFO can show Belady's anomaly.
  • In LRU, updating recency only on faults; every hit also makes a page most recently used.

Worked examples

Try each step yourself, then reveal the working.

Example 1: Page-table size

numerical

A system has 32-bit logical addresses, 4 KB pages, 1 GB of physical memory and 4-byte page-table entries. Find the number of page-table entries, the page-table size, and the number of bits in a frame number.

  1. Step 1: Find the offset bits.

    Try it on paper first.

Example 2: Effective memory access time with a TLB

numerical

TLB lookup takes 10 ns, a memory access takes 100 ns, and the TLB hit ratio is 90%. Paging is single-level. Find the effective memory access time.

  1. Step 1: Cost of a hit.

    Try it on paper first.

Example 3: FIFO, LRU and OPT on one string

numerical

Reference string: 1, 2, 5, 4, 3, 1, 3, 2, 3, 4, 3, 2. Three frames, initially empty. Count the page faults under FIFO, LRU and optimal replacement.

  1. Step 1: Trace FIFO.

    Try it on paper first.

Example 4: Why more frames can hurt FIFO

conceptual

Why can FIFO suffer Belady's anomaly while LRU cannot?

  1. Step 1: What does LRU keep with $n$ frames?

    Try it on paper first.

Previous year questions

20 official GATE CSE questions on this topic, newest first.

Practise these 20 PYQs →

Topic test

10 questions: up to 4 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE CSE syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.