Memory management is a fundamental part of an operating system that involves the allocation, tracking, and management of the computer's memory resources. Since multiple processes and programs may be running simultaneously, the operating system must ensure that each process gets the necessary memory without interfering with others.
Virtual memory is a technique that allows the execution of processes to be independent of the available physical memory, enabling more efficient use of memory and allowing programs to use more memory than is physically available.
Memory management in an operating system involves a series of processes and policies designed to optimize the use of the physical memory (RAM) and virtual memory, ensuring the smooth operation of multiple programs and processes.
The primary tasks of memory management include:
Memory allocation refers to the process of assigning portions of memory to programs and processes. There are several techniques used to allocate and deallocate memory:
In this technique, each process is allocated a single contiguous block of memory in the physical memory. This is the simplest and most straightforward memory allocation method, but it has drawbacks:
Paging divides physical memory into fixed-size blocks called pages and divides the logical memory of a process into blocks of the same size, called page frames. Pages are loaded into available frames, and the operating system uses a page table to keep track of which virtual page is loaded into which physical frame.
Segmentation divides memory into segments of varying sizes, such as code, data, and stack segments. Unlike paging, the segments are logical units of the program rather than fixed-size blocks. Each segment is assigned a segment table to map virtual addresses to physical addresses.
This is a hybrid approach combining paging and segmentation. The program is divided into segments, and each segment is further divided into pages. This helps combine the advantages of both paging (eliminating external fragmentation) and segmentation (providing logical separation of program components).
Virtual memory is a technique that allows the operating system to present the illusion of a larger memory space than is actually available in physical RAM. Virtual memory enables a system to run programs that require more memory than what is physically installed in the machine.
Virtual Memory Addressing: Each process is given the illusion of having its own continuous address space, even though the memory may be fragmented and stored across various locations in physical memory and secondary storage (disk).
Page Fault: When a process accesses a page that is not currently in physical memory (RAM), a page fault occurs. The operating system then fetches the required page from disk and loads it into memory. If the memory is full, the operating system must swap out a page to free space.
Swap Space: This is a dedicated area on the hard disk or SSD used to store pages that are not currently in memory. When the physical memory is full, pages are moved between physical memory and swap space.
Address Translation: Virtual addresses generated by the CPU are mapped to physical addresses using a hardware mechanism called the Memory Management Unit (MMU). The MMU uses the page table (in paging) or segment table (in segmentation) to perform this translation.
Page Table: A page table holds the mapping between a process’s virtual pages and the physical memory frames. If a page is not currently in memory, a page fault occurs, triggering the operating system to bring the page into memory from secondary storage.
Demand Paging: With demand paging, pages are only loaded into memory when they are needed, reducing the amount of physical memory required at any time.
Thrashing: If the system spends too much time swapping pages in and out of memory, a condition called thrashing occurs. This leads to poor performance because the system is constantly swapping pages between memory and disk, rather than executing processes.
TLB (Translation Lookaside Buffer): To optimize memory access speed, modern systems use a small, fast cache called the TLB to store recent translations of virtual addresses to physical addresses. The TLB reduces the time it takes to look up the mapping in the page table.
When a page fault occurs and there is no available space in physical memory, the operating system must decide which page to remove from memory. Several page replacement algorithms are used to select the victim page:
Memory management and virtual memory are crucial aspects of modern operating systems, enabling efficient utilization of both physical and virtual memory. Memory management includes techniques such as contiguous memory allocation, paging, and segmentation, while virtual memory allows programs to run using more memory than physically available. Despite its benefits, virtual memory can introduce challenges like thrashing and performance degradation if not managed properly. Effective memory management is essential for maintaining system performance and reliability, particularly in multi-tasking environments.
Open this section to load past papers