You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson covers memory management in depth, focusing on paging, segmentation, virtual memory and page replacement algorithms. The OCR H446 specification requires you to understand how the OS manages memory to allow multiple processes to run efficiently.
The OS must:
The OS divides virtual memory (the address space seen by each process) into fixed-size blocks called pages and divides physical RAM into fixed-size blocks called frames. Pages and frames are the same size (commonly 4 KB).
Virtual Memory (Process A) Physical RAM (Frames)
+--------+ +--------+
| Page 0 | ----+ | Frame 0 | <-- Process B Page 1
+--------+ | +--------+
| Page 1 | ----+---+ | Frame 1 | <-- Process A Page 0
+--------+ | | +--------+
| Page 2 | -+ | | | Frame 2 | <-- Process A Page 2
+--------+ | | | +--------+
| +---|------------> | Frame 3 | <-- Process A Page 1
| | +--------+
+------+-----------> | Frame 4 | <-- (Free)
+--------+
Each process has its own page table that maps virtual page numbers to physical frame numbers.
| Virtual Page | Physical Frame | Valid Bit | Dirty Bit |
|---|---|---|---|
| 0 | 1 | 1 | 0 |
| 1 | 3 | 1 | 1 |
| 2 | 2 | 1 | 0 |
| 3 | - | 0 | 0 |
When a process accesses a memory address:
| Advantage | Explanation |
|---|---|
| No external fragmentation | Pages can be placed in any free frame; they do not need to be contiguous |
| Simple allocation | The OS just needs to find any free frame |
| Effective virtual memory | Pages not currently needed can be stored on disk |
| Memory protection | Each process has its own page table; it cannot access another process's frames |
| Disadvantage | Explanation |
|---|---|
| Internal fragmentation | The last page of a process may not be completely full, wasting space within the frame |
| Page table overhead | Each process needs its own page table, which uses memory. For large address spaces, page tables can be very large |
| Translation overhead | Every memory access requires a page table lookup (mitigated by the TLB) |
The TLB is a small, fast cache inside the MMU that stores recent page-to-frame mappings. It dramatically speeds up address translation by avoiding a full page table lookup for frequently accessed pages.
Instead of dividing memory into fixed-size pages, segmentation divides it into variable-size segments based on the logical structure of the program.
| Segment | Typical Content |
|---|---|
| Code segment | The program's instructions |
| Data segment | Global and static variables |
| Stack segment | Function call frames, local variables, return addresses |
| Heap segment | Dynamically allocated memory |
Each segment has:
| Segment | Base Address | Limit |
|---|---|---|
| Code | 0x1000 | 4096 |
| Data | 0x3000 | 2048 |
| Stack | 0x5000 | 1024 |
| Advantage | Explanation |
|---|---|
| Matches program structure | Each segment corresponds to a logical unit (code, data, stack) — intuitive for programmers and compilers |
| No internal fragmentation | Segments are exactly the size needed |
| Sharing and protection | Individual segments can have different access permissions (e.g. code segment can be read-only and shared between processes) |
| Disadvantage | Explanation |
|---|---|
| External fragmentation | As segments are created and destroyed, gaps of unusable free memory appear between them |
| Complex allocation | The OS must find a contiguous block of free memory large enough for each segment |
| Variable size management | More complex than paging because segments differ in size |
| Feature | Paging | Segmentation |
|---|---|---|
| Division basis | Physical (fixed-size blocks) | Logical (variable-size program sections) |
| Block size | Fixed (e.g. 4 KB) | Variable |
| External fragmentation | None | Yes |
| Internal fragmentation | Yes (last page) | None |
| Programmer visibility | Invisible to the programmer | Reflects program structure |
| Address translation | Page table | Segment table |
| Sharing | Page-level sharing (less intuitive) | Segment-level sharing (natural for code sharing) |
Many modern systems use segmented paging — the address space is first divided into segments, and then each segment is divided into pages. This combines the logical structure of segmentation with the efficient memory management of paging.
Virtual memory allows processes to use more memory than the physical RAM available by storing inactive pages on secondary storage (the swap file or page file).
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.