🔒 Pro · Component 1 · 1.2 Software and Software Development
1.2.1b Memory Management: Paging, Segmentation & Virtual Memory
OCR H446 · A Level Computer Science · ~13 min read
Notes
Video
Slides
Worksheet
Quiz

Why Memory Management is Needed

Modern computers run many processes simultaneously, but physical RAM is finite. The OS must manage how RAM is shared between processes, prevent one process corrupting another's memory, and extend apparent memory capacity using disk storage. Three key techniques: paging, segmentation, and virtual memory.

Paging

Paging divides physical memory (RAM) and logical process memory (virtual address space) into fixed-size blocks:

  • Page: fixed-size block of logical (virtual) address space.
  • Frame: fixed-size block of physical RAM (same size as a page).
  • Pages are mapped to frames — a page can be stored in any free frame, so they do not need to be contiguous in RAM.
  • The OS maintains a page table for each process, recording which frame each page is currently stored in.
  • When a program accesses a virtual address, the MMU (Memory Management Unit) uses the page table to translate the virtual page number to the physical frame number.

Advantages of paging: eliminates external fragmentation (pages fit exactly into frames); simple allocation algorithm (any free frame will do).

Disadvantages: internal fragmentation (the last page may not be full); overhead of maintaining page tables.

Segmentation

Segmentation divides a process's memory into variable-size logical segments corresponding to the program's structure (code segment, data segment, stack segment, heap segment).

  • Each segment has a base address (where it starts in physical memory) and a limit (its size).
  • A segment table stores base and limit for each segment.
  • Segments must be allocated contiguously within memory, but different segments can be placed anywhere in RAM.

Advantages: reflects natural program structure; segments can grow independently; easier to share code segments between processes.

Disadvantages: variable sizes cause external fragmentation (gaps in memory that are too small to use); more complex allocation than paging.

Comparison: Paging vs Segmentation

FeaturePagingSegmentation
Block sizeFixed (e.g. 4 KB)Variable (based on program structure)
FragmentationInternal (wasted space within a page)External (gaps between segments)
Logical meaningNone — arbitrary divisionReflects logical program units (code, stack, data)
Contiguous?Pages can be non-contiguous in RAMEach segment must be contiguous
Lookup tablePage table (one per process)Segment table (one per process)

Virtual Memory

Virtual memory is a memory management technique that allows processes to use more memory than is physically available in RAM, by using a portion of secondary storage (hard drive or SSD) as an extension of RAM.

How virtual memory works:

  • The OS creates a virtual address space for each process, which is larger than physical RAM.
  • Only the parts of a process currently needed are loaded into RAM (pages are the unit used in modern systems — combining paging with virtual memory).
  • Pages not currently needed are stored in a swap space (a dedicated area of disk storage, also called the page file in Windows).
  • When a program accesses a page that is not in RAM (page fault), the OS: (1) pauses the process, (2) finds a frame to evict (using a replacement algorithm), (3) writes the evicted page to swap if it has been modified (dirty page), (4) loads the requested page from swap into the freed frame, (5) updates the page table, (6) resumes the process.

Page Replacement Algorithms

AlgorithmDescriptionNotes
FIFO (First In, First Out)Replace the page that has been in memory the longestSimple; can evict frequently used pages
LRU (Least Recently Used)Replace the page that was used least recentlyMore complex; better performance than FIFO
Optimal (OPT)Replace the page that won't be used for the longest time in the futureTheoretical best; impossible in practice (requires knowing the future)

Thrashing

Thrashing occurs when the OS spends more time swapping pages in and out of RAM than actually executing process instructions. It happens when a process or set of processes requires more frames than available, causing a constant cycle of page faults. Thrashing severely degrades system performance. Solutions: add more RAM; reduce the number of running processes; use a working set model.

Exam tip: Know the distinction: paging = fixed-size blocks, no external fragmentation; segmentation = variable-size blocks, external fragmentation but logical meaning. Virtual memory uses paging to extend RAM using disk. Thrashing = too many page faults = system slowdown.
Exam tip: In exam questions on page faults, describe the full sequence: page fault detected → locate page in swap → find/evict a frame → load page → update page table → resume process.
⚠ Common Mistakes
  • Saying virtual memory IS RAM — it is secondary storage used to extend the apparent size of RAM.
  • Confusing internal and external fragmentation — internal is wasted space inside allocated blocks (paging); external is unusable gaps between blocks (segmentation).
  • Saying paging means pages are contiguous — they are NOT. Pages can be placed in any free frame anywhere in RAM.
✓ Notes completed!
Video coming soon
Click to advance · Arrow keys also work
Click slide or press arrow keys to navigate

Worksheet — 1.2.1b Memory Management

8 questions · 20 marks · instantly marked

Q1Explain the difference between a page and a frame in the context of paging.[2 marks]
✓ Mark scheme
A page is a fixed-size block of a process's logical (virtual) address space [1]; a frame is a fixed-size block of physical RAM of the same size as a page; pages are mapped to frames [1].
Q2Explain what a page table is and describe its role in translating virtual addresses to physical addresses.[3 marks]
✓ Mark scheme
A page table is a data structure maintained by the OS for each process, recording which physical frame in RAM each virtual page is currently stored in [1]; when a process accesses a virtual address, the MMU extracts the page number from the address [1] and looks up the corresponding frame number in the page table to determine the physical location in RAM [1].
Q3Distinguish between internal fragmentation (paging) and external fragmentation (segmentation).[4 marks]
✓ Mark scheme
Internal fragmentation (paging): occurs when a process's last page is not full — the remaining space in that page/frame is wasted but cannot be used by another process [1]; it occurs because pages are fixed-size and a process's data rarely fills the last page exactly [1]; external fragmentation (segmentation): occurs when free memory is split into small non-contiguous gaps between allocated segments [1]; none of these gaps may be large enough to fit a new segment even if total free memory is sufficient [1].
Q4Describe what happens when a page fault occurs in a virtual memory system.[4 marks]
✓ Mark scheme
The process attempts to access a virtual page that is not currently loaded into RAM [1]; the MMU raises a page fault interrupt and the OS pauses the process [1]; the OS selects a page in RAM to evict (using a replacement algorithm); if that page has been modified it is written to swap space on disk [1]; the required page is then loaded from swap into the freed frame, the page table is updated, and the process resumes from the instruction that caused the page fault [1].
Q5Explain what thrashing is and describe one way it can be prevented.[3 marks]
✓ Mark scheme
Thrashing occurs when the OS spends more time swapping pages in and out of RAM than executing process instructions [1]; it happens when processes collectively require more frames than are available in RAM, causing a constant cycle of page faults [1]; prevention: install more physical RAM [1] / reduce the number of concurrent processes [1] / use a working set model to ensure each process has sufficient frames [1].
Q6Explain one advantage segmentation has over paging in terms of sharing code between processes.[2 marks]
✓ Mark scheme
In segmentation, the code segment (executable instructions) can be marked as read-only and shared between multiple processes [1]; two processes running the same program (e.g. two instances of a browser) can share a single copy of the code segment in memory, saving RAM [1].
Q7Compare the LRU and FIFO page replacement algorithms. Which is generally considered superior and why?[3 marks]
✓ Mark scheme
FIFO replaces the page that has been in memory the longest, regardless of how recently or frequently it was used [1]; LRU replaces the page that was used least recently, approximating the page that is least likely to be needed soon [1]; LRU is generally superior because it takes into account the temporal locality of memory access — recently used pages are more likely to be used again, so evicting the least recently used page reduces page fault rates [1].
Q8Explain why the Optimal (OPT) page replacement algorithm cannot be implemented in practice.[2 marks]
✓ Mark scheme
The Optimal algorithm requires knowing which pages will be requested in the future [1] so that it can evict the page that will not be needed for the longest time; this is impossible during normal operation as future page requests depend on program execution which cannot be known in advance [1].
Topic Quiz
1 of 15
You scored
out of 15
🎯

Mini Test — 1.2.1b Memory Management

  • 10 questions · 10 marks · 10 minutes
  • 5 MCQ + 5 short answer
Card 1 of 15
Click to reveal
🎉
Complete!
TermDefinition
← 1.2.1a OS Functions 1.2.1 Systems Software Next: 1.2.1c Interrupts & Scheduling →
🔒
Pro Content
Subscribe to access all 69 OCR H446 A Level lessons.
£7.99/month
or £59/year
Subscribe now →