Memory Management (OCR A-Level Computer Science): Revision Notes
📚 Revision Notes
Memory Management
Overview
Memory management is a critical function of the operating system that involves efficiently allocating, tracking, and freeing up memory resources in a computer system. Effective memory management ensures that multiple processes can run smoothly without conflicts or slowdowns. To manage memory, operating systems use several techniques, including paging, segmentation, and virtual memory. Understanding these methods helps explain how an OS optimises memory use, maintains stability, and enables multitasking.
Paging
- Definition: Paging is a memory management scheme that divides the physical memory (RAM) into small, fixed-size blocks called pages. Each programme's memory is also divided into pages of the same size, called page frames.
- Purpose and Function: Paging enables the OS to allocate non-contiguous memory locations to a process, meaning a programme's data doesn't have to be stored in a single block. This flexibility prevents fragmentation, as pages can be placed wherever there is free space in memory.
- How It Works:
- When a programme is loaded, its data and code are split into pages, which can be loaded into any available page frames in RAM.
- The OS maintains a page table for each process, mapping the programme's pages to the physical memory page frames.
- Paging in Virtual Memory: Paging is essential in virtual memory systems (explained further below), allowing the OS to move inactive pages to disc and retrieve them as needed.
Segmentation
- Definition: Segmentation is a memory management technique where memory is divided into variable-sized segments based on the logical structure of the programme (e.g., code, data, stack).
- Purpose and Function: Segmentation reflects the logical divisions within a programme, allowing for easier organisation and protection of data. Each segment represents a specific part of the programme, and segments can vary in size.
- How It Works:
- The OS assigns each segment a unique segment number and offset, which the CPU uses to locate the segment in memory.
- Segmentation allows processes to access segments more logically, reducing the need to fit data into fixed sizes like in paging.
- Benefits: This method can improve memory access efficiency, as it allows for larger blocks of memory to be allocated for specific purposes (like code or stack) rather than fixed pages.
Virtual Memory
- Definition: Virtual memory is a technique that extends a computer's physical memory by using disc space as additional "virtual" RAM. This allows more processes to run than the physical memory would typically allow.
- Purpose and Function: Virtual memory ensures that when physical RAM is full, parts of inactive processes can be temporarily stored on the disc in a section called the swap space. This gives the illusion of a larger main memory, allowing more applications to run simultaneously.
- How Paging Is Used in Virtual Memory:
- When RAM is full, the OS swaps some pages of inactive processes from RAM to disc (swap space).
- When a swapped-out page is needed, it is moved back into RAM, potentially swapping out another page to make room.
- This process of moving pages between RAM and disc is called paging and allows the system to handle more processes than physical memory alone would support.
- Benefits and Drawbacks of Virtual Memory:
- Benefits:
- Supports multitasking: Allows more processes to be open simultaneously.
- Increases programme size capability: Programmes larger than the available RAM can still run.
- Drawbacks:
- Slower access: Accessing data from disc is significantly slower than from RAM, potentially causing delays when too much swapping occurs (known as thrashing).
- Increased wear on storage: Frequent swapping can wear down the storage device over time, particularly on SSDs.
Examples
Paging
- Suppose a word processor uses 10 pages, but only five-page frames are available in RAM.
- The OS loads five of the word processor's pages into the RAM, and the remaining five stay on the disc.
- As the user works, pages can be swapped in and out of RAM as needed.
Segmentation
- An application has three segments: code (instructions), data, and stack (temporary storage).
- These segments are stored in separate parts of memory, which the OS accesses using segment numbers.
- Segmentation helps the OS organise memory more logically, keeping similar data close together.
Virtual Memory
- A computer with 4GB of RAM and 500GB of disc space can use part of the disc as virtual memory.
- If the user opens a large video editing programme that requires 6GB of memory, the OS will move inactive pages from other applications to the disc, freeing up enough RAM for the video editing programme.
Note Summary
infoNote
Common Mistakes
- Confusing Paging with Segmentation: Paging divides memory into fixed-sized pages, while segmentation divides memory based on logical sections of a programme. Paging is about uniform size, while segmentation reflects programme structure.
- Assuming Virtual Memory Is the Same as RAM: Virtual memory is an extension of RAM but is much slower because it uses disc storage. It provides the benefit of running more applications but at a potential cost of speed.
- Overlooking the Role of the Page Table: Many students overlook the importance of the page table in paging, which maps virtual pages to physical memory locations.
- Misunderstanding Thrashing: Thrashing occurs when too much swapping between RAM and disc happens, leading to system slowdowns. It's often mistakenly thought to be caused by insufficient disc space, but it's actually due to excessive virtual memory usage.
infoNote
Key Takeaways
- Paging: Divides memory into fixed-size pages, mapped by a page table, allowing flexibility and reducing fragmentation. Essential for virtual memory.
- Segmentation: Divides memory into logical segments of variable sizes, reflecting programme structure and improving access efficiency.
- Virtual Memory: Extends RAM by using disc space, enabling more programmes to run than would otherwise fit in RAM. Uses paging to swap data between RAM and disc.
- Advantages: Supports multitasking and larger programmes.
- Disadvantages: Slower performance due to disc access and risk of thrashing with high memory demand.