Skip to main content

Operating Systems

Memory Management

Memory-management marks come from arithmetic: bits in a logical address, page-table size, number of levels and effective access time. Master the formulas and the difference between paging and segmentation.

~5 hrs ~1–1.5 marks · High 9 samples · 8 practice · 8 cards

GATE CSE memory management — address binding, contiguous allocation and fragmentation, first/best/worst fit, paging with page-table size and multilevel bits, TLB and effective access time, segmentation and segmented paging, with worked examples, samples and drills.

Learning outcomes

  • Split a logical address into page number and offset and compute page-table size and frame count
  • Design multilevel page tables so that each level fits in one page and count the levels
  • Compute effective memory access time with and without a TLB
  • Compare fragmentation types, placement policies, paging, segmentation and segmented paging

Address spaces and contiguous allocation

  • A LOGICAL (virtual) address is generated by the CPU; a PHYSICAL address is seen by the memory unit. The memory-management unit (MMU) maps logical to physical addresses at run time. With a relocation (base) register and limit register, physical = logical + base, and a logical address ≥ limit causes a trap.
  • Address binding can happen at compile time (absolute code), load time (relocatable code) or execution time (needed for swapping and paging). Execution-time binding requires hardware support (MMU).
  • CONTIGUOUS allocation gives each process one contiguous block. FIXED (static) partitions suffer from INTERNAL fragmentation (unused space inside an allocated partition); VARIABLE (dynamic) partitions suffer from EXTERNAL fragmentation (free memory split into holes too small to use).
  • Placement policies for variable partitions: FIRST FIT (first hole large enough; fast), NEXT FIT (resume search from last placement), BEST FIT (smallest adequate hole; leaves tiny useless holes and needs a full search), WORST FIT (largest hole; leaves large leftover holes). First fit and best fit are generally better than worst fit in speed and storage utilization.
  • COMPACTION shuffles memory contents to join free holes into one block; it needs dynamic (execution-time) relocation and is expensive. The 50-PERCENT RULE: with first fit, for every N allocated blocks about N/2 blocks are lost to fragmentation.
  • SWAPPING moves a process to a backing store and back; the major part of swap time is transfer time, proportional to the amount swapped.
  • Protection with base/limit registers: every address generated in user mode is checked against the limit; only the OS can load the registers.
  • Buddy system: memory is split into power-of-two blocks; a request is rounded up to the next power of two (internal fragmentation) and free buddies are coalesced quickly.
  • Buddy example: a request of 70 KB gets a 128 KB block (internal fragmentation 58 KB); the buddy of the block at address A of size 2^k is at A XOR 2^k.
  • Measuring fragmentation: external fragmentation = (total free memory) - (largest hole) when a request fails although total free ≥ request. In the example, after 212K, 417K and 112K are placed by first fit (or worst fit) 959K is still free, yet the 426K request fails because the largest remaining hole is only 300K.
  • Limit-register check: a logical address a is legal if 0 ≤ a < limit; then physical = base + a. Example: base 300040, limit 120900: logical 120899 → 420939 (legal), logical 120900 → trap.
  • Overlays and dynamic loading/linking: overlays let a program larger than memory keep only the needed part in memory; dynamic loading loads a routine when first called; dynamic linking (shared libraries) postpones linking until run time and needs OS support.
PolicyIdeaRemark
First fitfirst hole that fitsfast, usually good utilization
Next fitfirst fit starting from last allocationspreads allocations
Best fitsmallest hole that fitsslow, many tiny holes
Worst fitlargest holeleaves large leftovers, often worst

Worked example

Problem. Partitions of 100K, 500K, 200K, 300K, 600K (in order) must hold processes of 212K, 417K, 112K, 426K (in order). Which placement policies can place all of them?

Solution. First fit: 212 → 500 (rest 288), 417 → 600 (rest 183), 112 → 288 (rest 176), 426 → no hole ≥ 426 → must wait. Best fit: 212 → 300, 417 → 500, 112 → 200, 426 → 600 → all placed. Worst fit: 212 → 600 (rest 388), 417 → 500, 112 → 388, 426 → none → waits. Only best fit places all four.

Five free holes with first-fit, best-fit and worst-fit placement of four requests, and diagrams of internal and external fragmentation.
Placement policies on the same holes: only best fit places all four requests. Internal fragmentation is inside a block, external fragmentation is between blocks.

Paging, page tables and the TLB

  • PAGING divides logical memory into fixed-size PAGES and physical memory into FRAMES of the same size (a power of two). It removes external fragmentation but has INTERNAL fragmentation in the last page of each process (average half a page).
  • A logical address of m bits and page size 2^d has page number p (m - d bits) and offset d (d bits). Number of pages = 2^(m - d). The page table (one per process) has one entry (PTE) per page holding the frame number plus valid, protection, reference and dirty bits. Physical address = frame number × page size + offset.
  • Number of frames = physical memory size / page size. Bits in a frame number = (physical address bits) - d. PTE size is at least the frame-number bits plus control bits, usually rounded to bytes.
  • Page-table size (single level) = number of pages × PTE size. For a 32-bit logical address, 4 KB pages and 4-byte PTEs: 2^20 entries × 4 B = 4 MB per process, which is too large to keep contiguously, so we use MULTILEVEL paging.
  • MULTILEVEL paging pages the page table itself. If every table is made to fit exactly in one page, entries per table = page size / PTE size, so index bits per level = log2(page size / PTE size). Levels needed = ceil(page-number bits / index bits per level). 32-bit, 4 KB, 4 B → 10 + 10 + 12.
  • INVERTED page table: one entry per physical FRAME (not per page) holding (process id, page number); size depends on physical memory, not logical space, but lookups need hashing. HASHED page tables are used for large address spaces.
  • TLB (translation look-aside buffer): small, fast associative cache of page → frame mappings. On a hit the translation needs only the TLB lookup; on a miss the page table must be read from memory. The TLB must be flushed (or carry ASIDs) on a process context switch.
  • Shared pages: pages of reentrant code (editors, libraries) can be shared by mapping the same frames into several page tables. Protection bits (read/write/execute) and a valid-invalid bit are stored in each PTE.
  • Hardware: a page-table base register (PTBR) points to the table; a page-table length register (PTLR) bounds it. On a context switch the PTBR is reloaded, which is why the TLB must be flushed or tagged with address-space identifiers (ASIDs).
  • Internal fragmentation of the last page: a process of s bytes with page size p occupies ceil(s / p) pages and wastes ceil(s / p) × p - s bytes. Example: s = 72,766 bytes and p = 2048 B gives 36 pages (35 full pages hold 71,680 B) and wastes 36 × 2048 - 72,766 = 962 bytes; the worst case is p - 1 and the average is about p / 2.
  • A PTE of size e that must hold a frame number of f bits plus control bits needs e × 8 ≥ f + control bits; a typical question gives the physical memory size and asks the minimum PTE size (round up to bytes).
  • Choosing page size: larger pages shrink the page table and raise TLB reach but increase internal fragmentation and the cost of a page fault; the table-plus-fragmentation overhead s × e / p + p / 2 is minimised at p = sqrt(2 × s × e).

Formulas to remember

  • number of pages = logical address space / page size = 2^(m - d)
  • page-table size = (number of pages) × (PTE size)
  • EMAT (single-level, TLB) = h × (t_tlb + t_mem) + (1 - h) × (t_tlb + 2 × t_mem)
  • EMAT (k-level, TLB miss) = h × (t_tlb + t_mem) + (1 - h) × (t_tlb + (k + 1) × t_mem)
  • without TLB: access time = (k + 1) × t_mem for k-level paging

Worked example

Problem. A system has a 32-bit logical address, 4 KB pages and 4-byte page-table entries; physical addresses are 36 bits. Find the page-table size, the frame-number bits and the EMAT with TLB hit ratio 90%, TLB time 10 ns and memory time 100 ns.

Solution. Offset = 12 bits, page number = 20 bits → 2^20 pages; page table = 2^20 × 4 B = 4 MB. Frame number = 36 - 12 = 24 bits. EMAT = 0.9 × (10 + 100) + 0.1 × (10 + 100 + 100) = 99 + 21 = 120 ns.

A 16-bit logical address split into page number 2 and offset 100 is translated through a page table that maps page 2 to frame 5, giving physical address 5220.
Paging translation: the offset is copied unchanged, only the page number is replaced by a frame number.

TLB, EMAT and multilevel paging numericals

  • Step 1 for any EMAT question: write the hit time and the miss time as sums of memory accesses. One-level paging: hit = t_tlb + t_mem, miss = t_tlb + 2 × t_mem. Two-level: miss = t_tlb + 3 × t_mem. k levels: miss = t_tlb + (k + 1) × t_mem.
  • Some questions say the TLB is searched IN PARALLEL with (or its time is negligible compared to) the page-table access; then use t_tlb = 0 or overlap as stated. Always follow the wording: “TLB lookup time is 10 ns” means add it on both paths.
  • Solving for the hit ratio: EMAT = h × 110 + (1 - h) × 210 ≤ 140 gives 210 - 100h ≤ 140, so h ≥ 0.7. Solving for the required hit ratio is a frequent twist.
  • MULTILEVEL SIZING: choose index bits so each table fits exactly in one page: entries per table = page size / PTE size. For a 64-bit address, 4 KB pages (12 offset bits) and 8-byte PTEs: 512 entries per page, 9 bits per level, 52 page-number bits need ceil(52 / 9) = 6 levels.
  • MEMORY USED by page tables depends on how many pages are actually mapped: a two-level scheme allocates inner tables only for used regions. A process that uses 8 MB contiguous in a 32-bit space with 4 KB pages needs 2048 pages = 2 inner tables + 1 outer table = 3 pages = 12 KB, versus 4 MB for a flat table.
  • INVERTED page table: size = (number of physical frames) × (entry size), independent of the number of processes. One lookup uses a hash on (pid, page number), so the lookup time is that of hashing plus the chain; sharing pages is harder.
  • Memory-reference count: without a TLB a k-level scheme needs k + 1 memory accesses per reference; with a perfect TLB only 1. The TLB therefore turns a 2-level scheme from 300 ns to about 100 ns plus the lookup.
Scenario (t_tlb = 10, t_mem = 100 ns)CalculationEMAT
No TLB, 1 level2 × 100200 ns
No TLB, 2 levels3 × 100300 ns
TLB h = 0.9, 1 level0.9 × 110 + 0.1 × 210120 ns
TLB h = 0.9, 2 levels0.9 × 110 + 0.1 × 310130 ns
TLB h = 0.98, 2 levels0.98 × 110 + 0.02 × 310114 ns
Hit ratio needed for 140 ns (1 level)210 - 100h ≤ 140h ≥ 0.7

Worked example

Problem. A 32-bit machine uses two-level paging with 4 KB pages and 4-byte PTEs, each table occupying exactly one page. A process maps 8 MB of contiguous virtual memory from address 0. How many bits index each level, and how much memory do its page tables use? Also find the EMAT for TLB hit ratio 0.95, t_tlb = 10 ns, t_mem = 100 ns.

Solution. Offset = 12 bits; entries per table = 4096 / 4 = 1024 = 2^10, so 10 bits for each of the two levels (10 + 10 + 12 = 32). 8 MB / 4 KB = 2048 pages need 2048 / 1024 = 2 inner tables plus 1 outer table = 3 tables × 4 KB = 12 KB. EMAT = 0.95 × (10 + 100) + 0.05 × (10 + 3 × 100) = 104.5 + 15.5 = 120 ns.

Flow of a memory reference through the TLB: a hit costs 110 ns, a miss costs 210 ns for one-level paging, giving EMAT 120 ns, or 130 ns with two-level paging.
TLB hit path vs miss path. EMAT = h × hit time + (1 - h) × miss time.

Segmentation and segmented paging

  • SEGMENTATION supports the user view of memory: a program is a collection of variable-size segments (main, procedures, stack, symbol table). A logical address is a pair <segment number s, offset d>.
  • The SEGMENT TABLE has one entry per segment: BASE (starting physical address) and LIMIT (segment length). Translation: if d < limit(s), physical address = base(s) + d; otherwise trap (addressing error).
  • Segmentation avoids internal fragmentation but suffers from EXTERNAL fragmentation (variable-size segments, like variable partitions). Segments are natural units for sharing and protection (code segment read-only).
  • Paging vs segmentation: pages are fixed-size and invisible to the programmer; segments are variable-size and visible. Paging has internal fragmentation, segmentation has external fragmentation. Paging needs one address (page, offset) split by hardware, segmentation needs the programmer or compiler to supply the segment.
  • SEGMENTED PAGING (e.g. Intel x86, MULTICS): each segment is divided into pages, so segmentation provides logical structure and sharing while paging removes external fragmentation. A logical address goes through the segment table (gives the page-table base of that segment), then the page table, then the frame.
  • Each segment of a segmented-paging system has its own page table whose size is limited by the segment limit, so page tables are small for small segments.
  • Worked check: segment table entry segment 2 has base 4300 and limit 400. Logical address <2, 53> maps to 4300 + 53 = 4353; address <2, 500> is beyond the limit and traps.
  • Memory-management hardware summary: base-limit (contiguous), page table + TLB (paging), segment table (segmentation), both (segmented paging).
FeaturePagingSegmentation
Unit sizefixed (page / frame)variable (segment)
Visible to programmernoyes
Fragmentationinternalexternal
Address(page number, offset)(segment number, offset)
Table entryframe number + bitsbase + limit

Worked example

Problem. A process uses 3-level paging with 4 memory accesses and no TLB. What is the memory access time if t_mem = 100 ns? What if a TLB with a 100% hit ratio and zero lookup time is added?

Solution. Without a TLB each reference needs 3 page-table accesses plus the data access: 4 × 100 = 400 ns. With a perfect TLB the translation is free and only the data access remains: 100 ns.

Exam tips

  • Always start with: offset bits = log2(page size); page-number bits = address bits - offset bits.
  • Page-table size = number of pages × PTE size; frames = physical memory / page size.
  • Multilevel: index bits per level = log2(page size / PTE size) when each table fits one page.
  • EMAT without TLB for k-level paging = (k + 1) × t_mem; with TLB use hit and miss branches.
  • Internal fragmentation: paging and fixed partitions. External fragmentation: segmentation and variable partitions.
Exam pattern · 9 questions

GATE sample questions & answers

9 GATE-style MCQs for Memory Management with full explanations. Attempt each question before revealing the answer.

Q1 Standard GATE page-table size

A system has a 32-bit logical address, 4 KB pages and 4-byte page-table entries. The size of the single-level page table of a process is:

  • A) 4 KB
  • B) 1 MB
  • C) 4 MB
  • D) 16 MB
Show answer & explanation

Answer: C

Offset = 12 bits so there are 2^20 pages; table size = 2^20 × 4 bytes = 4 MB.

Q2

TLB access time is 20 ns, memory access time 100 ns and the TLB hit ratio is 80%. With single-level paging the effective memory access time is:

  • A) 120 ns
  • B) 130 ns
  • C) 140 ns
  • D) 160 ns
Show answer & explanation

Answer: C

Hit: 20 + 100 = 120 ns. Miss: 20 + 100 + 100 = 220 ns. EMAT = 0.8 × 120 + 0.2 × 220 = 96 + 44 = 140 ns.

Q3

Which of the following suffers from internal fragmentation?

  • A) segmentation
  • B) variable partitioning
  • C) paging
  • D) compaction
Show answer & explanation

Answer: C

The last page of a process is rarely full, wasting space inside an allocated frame. Segmentation and variable partitions have external fragmentation.

Q4

Which memory-placement policy places a process in the largest available hole?

  • A) first fit
  • B) best fit
  • C) worst fit
  • D) next fit
Show answer & explanation

Answer: C

Worst fit always picks the largest hole, hoping the leftover is large enough to reuse.

Q5

A 40-bit logical address uses 8 KB pages. The number of bits for the page number and the offset are respectively:

  • A) 27 and 13
  • B) 28 and 12
  • C) 13 and 27
  • D) 26 and 14
Show answer & explanation

Answer: A

8 KB = 2^13, so the offset is 13 bits and the page number is 40 - 13 = 27 bits.

Q6

In a segmented system, segment 2 has base 4300 and limit 400. The physical address of logical address (segment 2, offset 53) is:

  • A) 4353
  • B) 4247
  • C) 4700
  • D) trap (illegal)
Show answer & explanation

Answer: A

Offset 53 is less than limit 400 so the address is valid: 4300 + 53 = 4353.

Q7

A 32-bit logical address uses 4 KB pages and 4-byte entries with two-level paging where each table fits exactly in one page. The number of bits used for the outer (first-level) index is:

  • A) 8
  • B) 10
  • C) 12
  • D) 20
Show answer & explanation

Answer: B

Entries per page = 4096 / 4 = 1024 = 2^10, so each level uses 10 bits; 32 = 10 + 10 + 12.

Q8

The number of entries in an inverted page table is proportional to:

  • A) the size of the logical address space
  • B) the number of processes
  • C) the number of physical frames
  • D) the page size only
Show answer & explanation

Answer: C

An inverted page table has one entry per physical frame, so its size depends on physical memory.

Q9

A system uses 3-level paging with no TLB. If one memory access takes 100 ns, one logical memory reference takes:

  • A) 100 ns
  • B) 300 ns
  • C) 400 ns
  • D) 500 ns
Show answer & explanation

Answer: C

Three page-table accesses plus one data access = 4 × 100 = 400 ns.

Topic drill · 8 questions

8 practice questions — Memory Management

Extra page-specific practice beyond the GATE samples. Attempt all 8, then reveal answers to check your reasoning.

P1

A process is 72766 bytes and the page size is 2048 bytes. The internal fragmentation in its last page is:

  • A) 962 bytes
  • B) 1086 bytes
  • C) 0 bytes
  • D) 2048 bytes
Show answer & explanation

Answer: A) 962 bytes

Pages needed = ceil(72766 / 2048) = 36; 36 × 2048 = 73728; waste = 73728 - 72766 = 962 bytes.

P2

Physical memory is 64 MB and the page size is 4 KB. The number of frames is:

  • A) 2^12
  • B) 2^14
  • C) 2^16
  • D) 2^26
Show answer & explanation

Answer: B) 2^14

64 MB / 4 KB = 2^26 / 2^12 = 2^14 = 16384 frames.

P3

TLB lookup takes 10 ns, memory access 90 ns and the hit ratio is 0.9 (single-level paging). The EMAT is:

  • A) 100 ns
  • B) 109 ns
  • C) 110 ns
  • D) 190 ns
Show answer & explanation

Answer: B) 109 ns

Hit: 100 ns, miss: 10 + 90 + 90 = 190 ns. EMAT = 0.9 × 100 + 0.1 × 190 = 90 + 19 = 109 ns.

P4

With 1 KB pages, logical address 3000 maps to page number and offset:

  • A) page 2, offset 952
  • B) page 3, offset 0
  • C) page 2, offset 1024
  • D) page 1, offset 1976
Show answer & explanation

Answer: A) page 2, offset 952

3000 / 1024 = 2 remainder 3000 - 2048 = 952.

P5

In the previous question page 2 is mapped to frame 7. The physical address is:

  • A) 7168
  • B) 8120
  • C) 9000
  • D) 3000
Show answer & explanation

Answer: B) 8120

7 × 1024 + 952 = 7168 + 952 = 8120.

P6

A 48-bit virtual address, 4 KB pages and 8-byte page-table entries are used with each page table fitting exactly in one page. The number of levels is:

  • A) 3
  • B) 4
  • C) 5
  • D) 6
Show answer & explanation

Answer: B) 4

Entries per page = 4096 / 8 = 512 = 2^9, so 9 bits per level. Page-number bits = 48 - 12 = 36, and 36 / 9 = 4 levels.

P7

If the page size is increased (other parameters unchanged), which of the following is true?

  • A) page-table size increases and internal fragmentation decreases
  • B) page-table size decreases and internal fragmentation increases
  • C) both increase
  • D) both decrease
Show answer & explanation

Answer: B) page-table size decreases and internal fragmentation increases

Larger pages mean fewer pages (smaller table) but more wasted space in the last page.

P8

Which technique removes external fragmentation by moving processes in memory?

  • A) paging
  • B) swapping
  • C) compaction
  • D) overlays
Show answer & explanation

Answer: C) compaction

Compaction relocates processes to merge holes into a single block; it needs run-time relocation.

Topic FAQs

How is the page-table size calculated?

Number of pages (2^(address bits - offset bits)) multiplied by the PTE size. For 32-bit addresses, 4 KB pages and 4-byte entries it is 2^20 × 4 B = 4 MB.

Why is multilevel paging used?

A single-level table for a large logical address space is huge and must be contiguous. Multilevel paging splits the table into page-sized pieces and keeps only the needed parts in memory, saving space.

What is the effective access time with a TLB?

EMAT = h × (t_tlb + t_mem) + (1 - h) × (t_tlb + (levels + 1) × t_mem) for a hit ratio h; with single-level paging the miss cost is t_tlb + 2 × t_mem.

Which fragmentation does paging have?

Only internal fragmentation in the last page of a process. External fragmentation does not occur because every free frame can be used.

Quick revision · 8 cards

8 flashcards — Memory Management

Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.

Q1 How do you split a logical address in paging?
Q2 Page-table size formula?
Q3 Internal vs external fragmentation?
Q4 EMAT with TLB for single-level paging?
Q5 What is an inverted page table?
Q6 Segment-table entry contents?
Q7 Which placement policy is usually best?
Q8 What does segmented paging combine?