Computer Science and IT‹›⌂ 0:00 studied

Module 9 · 17 min read · Free chapter

Memory management and virtual memory

In short

Paging splits an address into page number and offset and maps pages to frames. With a TLB, effective access time is h(t + m) + (1 − h)(t + 2m). Page faults are costly, so the fault rate must be tiny. OPT gives the fewest faults, LRU approximates it, and FIFO can show Belady's anomaly.

What are you preparing for? We show the practice and previous year questions of your exam.

Start from zero: a small desk and a big shelf

Think of a library reading room. Your desk can hold only four books, but for your project you need forty, which stay on the tall shelves behind you. You keep the books you are using now on the desk, and when you need another, you put one back and fetch the new one.

Main memory is the desk and the disk is the shelf. A program may be far bigger than memory, yet it runs smoothly, because at any moment it uses only a few parts of itself. The operating system decides which parts sit in memory and quietly swaps the rest.

To do this well, the OS needs answers to three questions. How does a program's address turn into a real place in memory? How is memory shared between many programs without gaps going to waste? And when memory is full, which part should go back to the shelf?

This chapter answers all three, with the numericals that GATE asks.

Why it matters. Without virtual memory, every program would have to fit entirely in RAM and know where it was loaded. With it, your laptop runs dozens of programs whose total size is many times the RAM.

Address binding and contiguous allocation

A program refers to memory by addresses, and these must be bound to real locations at some point. Binding at compile time fixes the location in the code, binding at load time fixes it when the program is loaded, and binding at execution time lets the location change while the program runs. Only execution-time binding allows a process to be moved or swapped back to a different place.

The CPU produces a logical (virtual) address, and the memory-management unit (MMU) turns it into a physical address. In the simplest scheme the MMU holds two registers for the running process. It checks that the logical address is less than the limit register, traps if it is not, and adds the relocation (base) register to get the physical address.

In contiguous allocation each process gets one unbroken block. As processes come and go, memory becomes a pattern of used blocks and free holes, and a new request must be placed in a hole.

Strategy Rule Request of 280 KB, holes 150, 400, 250, 600, 300 KB
First fit first hole that is big enough 400 KB, leaving 120 KB
Best fit smallest hole that is big enough 300 KB, leaving 20 KB
Worst fit largest hole 600 KB, leaving 320 KB

First fit is fastest and usually as good as best fit in memory use. Best fit leaves tiny useless slivers, and worst fit leaves large holes that may still be useful.

Two kinds of waste appear. External fragmentation means the total free memory is enough, but it is split into holes too small to use; compaction shuffles processes together to merge the holes, which needs execution-time binding. Internal fragmentation means space is wasted inside a block, because the block given is bigger than the request.

Why it matters. Fragmentation is the main reason contiguous allocation was replaced by paging. Exam questions ask which fit a request goes to and which kind of fragmentation a scheme suffers from.

Paging and the page table

Paging cuts logical memory into fixed-size pages and physical memory into frames of the same size. Any page can go into any free frame, so a process no longer needs one unbroken block, and external fragmentation disappears. A page table for each process records which frame holds each page.

Page sizes are powers of two, which makes splitting an address trivial. If the logical address has mm bits and the page size is 2n2^n bytes, the high m−nm - n bits are the page number pp and the low nn bits are the offset dd. The MMU replaces pp by the frame number ff from the page table and keeps dd unchanged.

Worked example 1. Pages are 1 KB, so the offset has 10 bits. For logical address 2500, the page number is ⌊25001024⌋=2\lfloor \dfrac{2500}{1024} \rfloor = 2 and the offset is 2500−2048=4522500 - 2048 = 452. If the page table maps page 2 to frame 5, the physical address is

5×1024+452=55725 \times 1024 + 452 = 5572

Worked example 2. A system has 32-bit logical addresses, 4 KB pages and 4-byte page-table entries. The offset needs 12 bits, so the page number has 20 bits and there are 2202^{20} pages. A flat page table therefore needs 220×42^{20} \times 4 bytes = 4 MB for every process, even a tiny one.

Worked example 3. The same machine has 1 GB of physical memory. It holds 230212=218\dfrac{2^{30}}{2^{12}} = 2^{18} frames, so a frame number needs 18 bits.

Besides the frame number, each page-table entry carries control bits. The valid bit says whether the page belongs to the process and is in memory, protection bits allow read, write or execute, the dirty bit says the page was changed, and the reference bit says it was used recently.

Paging still has internal fragmentation, because the last page of a process is usually only partly full. On average the waste is half a page per process, which is why pages are not made very large.

Why it matters. Paging is how every modern operating system manages memory. Splitting an address into page number and offset is the first step of nearly every memory numerical in GATE.

The TLB, multilevel and inverted page tables

The page table lives in main memory, so a naive scheme needs two memory accesses for every data access: one to read the page-table entry and one to read the data. That would halve the speed of every program. The fix is a translation look-aside buffer (TLB), a small, very fast associative cache that holds recently used page-to-frame entries.

On a TLB hit the frame number comes from the TLB, and only one memory access is needed. On a miss the page table is read from memory and the entry is copied into the TLB. With TLB lookup time tt, memory access time mm and hit ratio hh, for a one-level table

EAT=h(t+m)+(1−h)(t+2m)\mathit{EAT} = h(t + m) + (1 - h)(t + 2m)

Worked example 4. Let t=20t = 20 ns, m=100m = 100 ns and h=0.8h = 0.8. Then EAT=0.8×120+0.2×220=96+44=140\mathit{EAT} = 0.8 \times 120 + 0.2 \times 220 = 96 + 44 = 140 ns, a slowdown of 40% over plain memory. Raising the hit ratio to 0.98 gives 0.98×120+0.02×220=1220.98 \times 120 + 0.02 \times 220 = 122 ns.

Multilevel page tables. A 4 MB flat table per process is wasteful when most of the address space is unused. The answer is to page the page table: a 32-bit address with 4 KB pages is split into 10 bits for the outer table, 10 bits for an inner table and 12 bits of offset. The outer table has 2102^{10} entries of 4 bytes, which is exactly one 4 KB page, and inner tables are created only for regions the process actually uses.

The price is time. With two levels, a TLB miss needs three memory accesses, two for the tables and one for the data, so the formula becomes h(t+m)+(1−h)(t+3m)h(t + m) + (1 - h)(t + 3m). The TLB is what makes this cost bearable.

An inverted page table turns the idea around. It has one entry for each physical frame in the whole system, holding the process ID and page number stored there, so its size depends on physical memory and not on the number of processes. Searching it is slow, so it is used with a hash table.

Why it matters. EAT with a TLB is among the most common GATE numericals in this module. Knowing how levels add memory accesses lets you adapt the formula to any question.

Segmentation

Programmers do not think of a program as a row of equal pages. They think of a code part, a global data part, a stack and a heap. Segmentation matches this view: memory is divided into variable-size segments, each a logical unit with its own name and length.

A logical address is a pair (s, d): segment number ss and offset dd within it. The segment table gives each segment a base, where it starts in physical memory, and a limit, its length. The hardware checks that d<limitd < \mathit{limit}, traps if not, and otherwise forms the physical address base+d\mathit{base} + d.

Worked example 5. Segment 1 has base 2000 and limit 600. The address (1, 450) is legal because 450<600450 < 600, and it maps to 2000+450=24502000 + 450 = 2450. The address (1, 700) fails the limit check, so the hardware traps to the OS with an addressing error.

Feature Paging Segmentation
Unit size fixed pages variable segments
Seen by programmer no yes
Fragmentation internal only external
Protection and sharing per page per logical unit, more natural

Because segments vary in size, segmentation brings back external fragmentation. Segmentation with paging solves this: each segment is itself paged, so the programmer keeps logical segments while memory is still handed out in frames.

Why it matters. Segmentation explains why a program can protect its code as read-only while its stack stays writable. The limit check is also a favourite one-step numerical.

Demand paging and page faults

Virtual memory lets a process run even when only part of it is in memory. Under demand paging, a page is loaded only when it is first used, and pages not in memory are marked invalid in the page table.

When the CPU touches an invalid page, the hardware raises a page fault, and the OS handles it in six steps. It checks that the reference is legal, finds a free frame, schedules a disk read of the page into that frame, waits while another process runs, updates the page table to valid, and restarts the instruction that faulted. If there is no free frame, a page must first be chosen for replacement, and if that page is dirty it must be written back to disk.

A page fault is very expensive, because a disk is about a hundred thousand times slower than memory. With page-fault rate pp, memory access time mm and fault service time ss,

EAT=(1−p) m+p s\mathit{EAT} = (1 - p)\,m + p\,s

Worked example 6. Let m=200m = 200 ns and s=8s = 8 ms =8,000,000= 8{,}000{,}000 ns. With p=0.001p = 0.001, EAT=0.999×200+0.001×8,000,000≈199.8+8000=8199.8\mathit{EAT} = 0.999 \times 200 + 0.001 \times 8{,}000{,}000 \approx 199.8 + 8000 = 8199.8 ns, about 8.2 μs. One fault in a thousand accesses makes memory about 40 times slower.

How small must pp be to keep the slowdown under 10%? We need 200+7,999,800 p<220200 + 7{,}999{,}800\,p < 220, so p<207,999,800≈2.5×10−6p < \dfrac{20}{7{,}999{,}800} \approx 2.5 \times 10^{-6}. That is fewer than one fault in about 400,000 accesses.

Demand paging works because programs show locality of reference: in any short period they use a small set of pages, such as one loop and the data it touches. Pure demand paging starts a process with no pages at all and lets the first faults bring them in.

Why it matters. This numerical shows why the page-fault rate must be tiny and why good replacement matters so much. It is also why adding RAM speeds up a slow computer more than almost anything else.

Page replacement: FIFO, optimal and LRU

When memory is full and a fault occurs, the OS must pick a victim page. The goal is the fewest page faults, and methods are compared on a reference string, the sequence of pages a process uses. We use 1, 3, 0, 3, 5, 6, 3, 1, 0, 3 with 3 frames, counting the first loads as faults.

  • FIFO replaces the page that has been in memory longest, like a queue.
  • Optimal (OPT) replaces the page that will not be used for the longest time in future, which gives the fewest faults but needs to know the future.
  • LRU (least recently used) replaces the page not used for the longest time in the past, using the past as a guide to the future.
Reference 1 3 0 3 5 6 3 1 0 3
FIFO F F F H F F F F F H
LRU F F F H F F H F F H
OPT F F F H F F H H F H

Worked example 7. FIFO has 8 faults: at 5 it evicts 1, at 6 it evicts 3, and then 3 must come back, evicting 0. LRU has 7 faults: at 6 it evicts 0, the least recently used, so 3 stays and hits. OPT has 6 faults: at 5 it evicts 0, which is needed latest, and at 6 it evicts 5, which is never needed again, so both 3 and 1 hit.

Belady's anomaly. You would expect more frames to mean fewer faults, but FIFO can break this rule. On the string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5, FIFO gives 9 faults with 3 frames and 10 faults with 4 frames.

LRU and OPT never show the anomaly because they are stack algorithms: the pages held with kk frames are always a subset of those held with k+1k + 1 frames. On the same string, LRU gives 10 faults with 3 frames and 8 with 4.

Exact LRU needs a timestamp or a stack updated on every memory access, which is costly. Real systems approximate it with the reference bit, as in the second-chance or clock algorithm.

Why it matters. Counting faults on a reference string is asked in almost every GATE paper. Belady's anomaly is a favourite one-line theory question.

Thrashing and the working set

Raising the number of processes in memory usually raises CPU use, because while one process waits for I/O another can run. But beyond a point each process has too few frames for the pages it is actively using. It faults, takes a frame from another process, which then faults too, and soon every process is waiting for the disk.

This collapse is thrashing: the system spends more time paging than doing useful work. A naive scheduler then sees low CPU use and admits even more processes, which makes it worse.

The cure is to give each process as many frames as its current locality needs. The working set W(t,Δ)W(t, \Delta) of a process is the set of distinct pages it referenced in the last Δ\Delta references. For example, if the last five references were 2, 6, 1, 5, 2 and Δ=5\Delta = 5, the working set is {1, 2, 5, 6}, of size 4.

If WSSi\mathit{WSS}_i is the working-set size of process ii, the total demand is D=∑WSSiD = \sum \mathit{WSS}_i. If DD exceeds the number of available frames, the OS suspends one process and frees its frames, and when demand falls it lets a new process in. Choosing Δ\Delta matters: too small misses the locality, and too large spans several localities.

A simpler control is the page-fault frequency method. The OS sets an upper and a lower bound on the fault rate, gives a process more frames when its rate is above the upper bound, and takes frames away when it is below the lower bound.

Why it matters. Thrashing explains why a computer with too little RAM becomes almost unusable rather than just a bit slower. The working-set idea is how the OS keeps the degree of multiprogramming in a safe range.

Descriptive lens: how written papers ask this

Written papers for IT officer posts, state Assistant Programmer exams and university-level tests ask about memory in four ways:

  1. Explain address translation in paging or segmentation with a labelled diagram.
  2. Compute page-table sizes, bit widths, effective access times with a TLB or with page faults.
  3. Trace page-replacement methods on a reference string and comment on the results.
  4. Discuss a problem and its cure, such as fragmentation and compaction, or thrashing and the working set.

Practice questions (our own, not previous-year questions).

  1. Explain paging with a diagram of address translation, and show how a TLB reduces the effective access time with a numerical example. (15 marks)
  2. For the reference string 7, 0, 1, 2, 0, 3, 0, 4 with 3 frames, find the number of faults under FIFO, LRU and optimal replacement. What is Belady's anomaly? (10 marks)
  3. What is thrashing? Explain how the working-set model and page-fault frequency control prevent it. (10 marks)

Answer plan for question 3.

  • Define thrashing as the state in which processes spend more time servicing page faults than executing.
  • Explain the cause, which is too many processes in memory so that each has fewer frames than its current locality needs.
  • Draw the graph of CPU utilisation against the degree of multiprogramming, showing the sharp fall after the peak.
  • Define the working set as the distinct pages referenced in the last Δ references and give a short example.
  • Explain that the OS compares the total working-set demand with available frames and suspends a process when demand is higher.
  • Describe page-fault frequency control with its upper and lower bounds, and conclude that both methods keep each process's locality in memory.

Quick revision

  • The MMU checks a logical address against the limit register and adds the base register.
  • First fit takes the first big-enough hole, best fit the smallest and worst fit the largest.
  • Compaction cures external fragmentation, and paging removes it but keeps internal fragmentation.
  • With an mm-bit address and page size 2n2^n, the page number has m−nm - n bits and the offset nn bits.
  • A 32-bit address with 4 KB pages and 4-byte entries needs a 4 MB flat page table.
  • With a TLB, EAT=h(t+m)+(1−h)(t+2m)\mathit{EAT} = h(t + m) + (1 - h)(t + 2m) for one level, and each extra level adds one mm on a miss.
  • In segmentation the offset must be less than the limit, and the physical address is base plus offset.
  • With page faults, EAT=(1−p)m+ps\mathit{EAT} = (1 - p)m + ps, so pp must be below about one fault in 400,000 accesses.
  • OPT is best, LRU is the practical approximation, and FIFO can show Belady's anomaly.
  • Thrashing is cured by giving each process its working set and reducing multiprogramming.

Check yourself

Exam-style questions on this chapter. Pick an answer to see the explanation. 0 of 15 answered

  1. Which binding lets a process be moved to a different memory location while it runs?

  2. Holes of 150, 400, 250, 600 and 300 KB exist in that order. A 280 KB request under best fit goes to:

  3. Compaction is a remedy for:

  4. With 1 KB pages, logical address 2500 has page number and offset:

  5. 32-bit logical addresses, 4 KB pages, 4-byte entries. The size of a flat page table is:

  6. With 4 KB pages and 1 GB of physical memory, the frame number needs:

  7. TLB time 20 ns, memory time 100 ns, hit ratio 0.8, one-level page table. Effective access time is:

  8. With a two-level page table and no TLB hit, one data access needs how many memory accesses?

  9. An inverted page table has:

  10. Segment 1 has base 2000 and limit 600. Logical address (1, 700) gives:

  11. Memory access 200 ns, page-fault service 8 ms, fault rate 0.001. Effective access time is about:

  12. Reference string 1, 3, 0, 3, 5, 6, 3, 1, 0, 3 with 3 frames. Page faults under LRU are:

  13. Belady's anomaly can occur with:

  14. The correct response to thrashing is to:

  15. With Δ=5\Delta = 5 and the last five references 2, 6, 1, 5, 2, the working-set size is:

Previous year questions

Previous year questions for UPSC CSE will appear here once each one is checked word for word against the official question paper.

Learn with AI

A tutor that asks instead of telling, a partner that argues the other side, and this chapter told through your own district.

Tell the tutor what you are stuck on. It will guide you with questions and give the answer after a few tries.

🙋 Raise a doubt or feedback

Stuck somewhere, or spotted a mistake? Write to the ExamHike team. We usually reply within a day, and you will see the reply on My progress.

Log in to ask a doubt or send feedback →

Frequently asked questions

How big is a page table for a 32-bit address with 4 KB pages?

There are 2²⁰ pages, so with 4-byte entries a flat page table needs 4 MB per process.

What is Belady's anomaly?

It is the rise in page faults when frames are increased, which can happen with FIFO but never with LRU or optimal replacement.

What is the difference between internal and external fragmentation?

Internal fragmentation is unused space inside an allocated block, while external fragmentation is free memory split into holes too small to use.

What is thrashing?

Thrashing is when processes spend more time paging than executing because they have fewer frames than their working sets need.

More in this module: Operating systems

Official sources

Facts in this chapter were checked against these official sources.

  1. GATE 2026 Computer Science and Information Technology (CS) syllabus, IIT Guwahati
  2. NPTEL Operating Systems (IIT Madras, 106106144): memory management and virtual memory