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 program'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 program'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 program 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 program'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 disk 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 program (e.g., code, data, stack).
- Purpose and Function: Segmentation reflects the logical divisions within a program, allowing for easier organisation and protection of data. Each segment represents a specific part of the program, 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 disk 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 disk 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 disk (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 disk 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 program size capability: Programs larger than the available RAM can still run.
- Drawbacks:
- Slower access: Accessing data from disk 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 disk.
- 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 disk space can use part of the disk as virtual memory.
- If the user opens a large video editing program that requires 6GB of memory, the OS will move inactive pages from other applications to the disk, freeing up enough RAM for the video editing program.
Note Summary