Memory management in computer systems refers to the process of controlling and coordinating main memory, which holds the instructions and data directly accessible by the processing units for executing different processes. Managing main memory is a complex task handled by the operating system, which assigns portions of memory, called blocks, to various running programs to optimize overall system performance. Over the years, this has been tackled using distinct strategies to ensure efficient utilization of resources.
Efficient Utilization: Ensures optimal use of RAM by allocating and deallocating memory as needed.
Process Isolation and Protection: Prevents processes from accessing each other’s memory space, maintaining system stability and security.
Multiprogramming Support: Enables multiple processes to reside in memory simultaneously, improving CPU utilization and throughput.
Tracking Memory Status: Maintains records of allocated and free memory locations, updating them as processes start and terminate.
Validation of requests: The memory manager checks the validity of a memory request and if the claimant has the proper permissions to request memory allocations
Allocation: Assigns memory to processes as required.
Deallocation: Frees memory when processes no longer need it, making it available for others.
Swapping: Temporarily moves inactive processes from main memory to secondary storage (disk) to free up RAM for active processes.
Address Translation: Converts logical addresses used by programs to physical addresses in RAM, often via hardware like the Memory Management Unit (MMU).
Defragmentation: The rearrangement of memory to establish more usable free memory portions
Logical and Physical Address Space
Logical Address Space: An address generated by the CPU is known as a logical address or virtual address. The logical address space can be defined as the range of addresses a process can use during execution. Logical addresses can change during program execution.
Physical Address Space: This refers to the set of actual addresses used by the memory hardware. A physical address, also called a real address, is generated by the Memory Management Unit (MMU) through run-time mapping of virtual addresses. Unlike virtual addresses, physical addresses remain constant.
The CPU generates the logical address(here, 324).
The MMU will generate the base address (here, 2000) which is stored in the Relocation Register.
The value of Relocation Register(here, 2000) is added to the logical address to get the physical address. i.e. 2000+324= 2324(Physical Address).
Difference between the physical and logical address
The fundamental difference between a physical address and the logical address is that logical address is generated by the CPU while the program is running whereas the physical address is a location in memory.
The logical address is generated by the CPU whereas physical address is computed by the MMU.
The logical address does not exist physically in the memory hence it is sometimes known as virtual address whereas the physical address is a location in the memory unit.
The logical address is used as a reference to access the physical address. The physical address cannot be accessed directly.
Users can view the logical address of a program. But, they cannot view the physical address of a program.
The set of all the logical addresses generated in reference to a program by the CPU is called Logical Address Space whereas the set of all the physical addresses mapped to the logical address is called Physical Address Space .
Static and Dynamic Loading
Loading a process into main memory is done by a loader. There are two types of loading:
Static Loading: This involves loading the entire program into memory at a fixed address before execution. It requires more memory space.
Dynamic Loading: This loads program routines into memory only when they are needed, saving memory by not loading unused routines. The routines remain on disk in a relocatable (loadable at any memory location) format until called. Dynamic loading allows better memory utilization, especially for large programs.
Memory mapping
Memory mapping refers to the allocation strategies to allocate a program to the main memory. In early systems, the general strategy was the straightforward mapping of a requested program to physical positions in contiguous spaces of the main memory. Thus, even memory addresses accessed by the programs corresponded to physical memory addresses.
As the programming possibilities increased, the programs grew bigger in terms of how much memory they occupy. Many programs, eventually, got bigger than the available memory in the computer systems. So, allocating an entire program in memory and direct mapping the program to physical memory positions turned unfeasible.
This context promoted the creation of virtual memory. With virtual memory, a program can be divided into multiple pieces mapped to virtual addresses. These pieces of programs, in turn, are on-demand allocated and deallocated in the physical memory, linking only the necessary virtual addresses to physical addresses.
The following image exemplifies a memory allocation in early systems and virtual memory-enabled systems:
Contiguous Memory Allocation
Contiguous Memory Allocation is a memory management technique where each process is allocated a single continuous block of memory. This approach is simple and efficient in terms of access speed because the CPU can easily calculate the address of any location within the block.
However, contiguous memory allocation suffers from fragmentation. External fragmentation occurs when there are sufficient total free memory spaces, but these are not contiguous, making it difficult to allocate large blocks of memory. Internal fragmentation happens when allocated memory blocks are slightly larger than the requested memory, leading to wasted space.
Additionally, the process of fitting processes into memory can be complex, requiring strategies like first-fit, best-fit, or worst-fit to manage available memory effectively.
Allocation Strategies
Allocation strategies in memory management determine how the operating system assigns free memory blocks (partitions or holes) to processes waiting in the input queue. These strategies are especially important in contiguous memory allocation, where efficient use of available memory can reduce fragmentation and improve system performance.
Common Allocation Strategies
When a process requests memory, the OS checks the list of available holes.
The chosen strategy determines which hole is selected for the allocation.
After allocation, if there is leftover space in the hole, it becomes a new smaller hole.
Internal Fragmentation: Occurs when allocated memory is larger than needed (common in fixed partitioning).
External Fragmentation: Happens when free memory is split into small blocks and cannot be used efficiently (common in dynamic partitioning with First Fit, Best Fit, and Worst Fit).
Paging
Paging is a memory management scheme that eliminates the need for contiguous allocation of physical memory. In paging, the physical memory is divided into fixed-size blocks called frames, and the logical memory (used by processes) is divided into blocks of the same size called pages. When a process is loaded into memory, its pages can be loaded into any available memory frames, and a page table keeps track of the mapping between the process’s pages and the physical frames.
This allows the physical address space to be non-contiguous, which solves the problem of external fragmentation and makes it easier to allocate memory efficiently. Paging also simplifies memory management by eliminating the need for complex allocation algorithms.
Page Table Structure
The page table is a data structure used in paging to store the mapping between virtual addresses and physical addresses. Each entry in a page table corresponds to a page of the process's logical address space and contains the address of the frame in physical memory where the page is stored.
Page tables can be implemented in several ways, including single-level, multi-level, and inverted page tables. A single-level page table is a simple array, but it can become very large if the address space is large.
Paging: Advantages and Disadvantages
Advantages:
Avoids gaps between allocated memory blocks, ensuring efficient use of available memory.
Pages can be placed anywhere in physical memory, simplifying memory allocation.
Fixed-size pages mean that memory allocation does not require complex algorithms.
Allows larger programs to run even with limited physical memory.
Virtual memory allows parts of a program to be stored on disk and brought into memory as needed.
Disadvantages:
Each process requires a page table, which can consume a lot of memory for large address spaces.
Each memory access requires a lookup in the page table, which can add delay.
Unused space in the last page of a process can lead to internal fragmentation.
Segmentation: Concept and Organisation
Segmentation is a memory management technique that divides the process's memory into variable-sized segments, each of which can be a logical unit such as a function, array, or data structure. Unlike paging, segments vary in length, reflecting the logical structure of the process.
Each segment has a segment number and a length, and memory addresses within a segment are specified by an offset from the segment's base address.
A segment table keeps track of each segment's base address and length. Segmentation facilitates sharing and protection, as segments can be independently protected and shared between processes, enhancing modularity and security.
Segmentation: Advantages and Disadvantages
Advantages:
Improves program readability and manageability.
Each segment can have its own access rights, enhancing security.
Reduces wasted space within segments.
Disadvantages:
Can suffer from external fragmentation.
Requires more complex algorithms for allocation and deallocation.
Virtual memory
Virtual memory is a memory management technique used by operating systems to create the illusion of a large, continuous block of memory for applications, even when the actual physical RAM is limited137. It enables computers to run programs that are larger than the available physical memory and supports efficient multitasking by allowing multiple applications to run simultaneously.
Illusion of Large Memory: Virtual memory uses a portion of secondary storage (such as a hard disk or SSD) to extend the apparent size of the main memory (RAM).
Address Translation: The operating system, with help from hardware like the Memory Management Unit (MMU), maps virtual addresses used by programs to physical addresses in RAM.
Swapping (Paging/Segmentation): When RAM is full, inactive data is temporarily moved (swapped) to disk storage (virtual memory). When that data is needed again, it is swapped back into RAM.
Implementation: Virtual memory is commonly implemented using paging (dividing memory into fixed-size pages) or segmentation (dividing memory into variable-sized segments).
Run Larger Programs: Programs can be larger than the available physical memory.
Multiprogramming: More programs can run at the same time, increasing CPU utilization and system throughput.
Memory Protection & Isolation: Each process operates in its own virtual address space, improving security and stability.
Efficient Memory Utilization: Only the necessary parts of programs are loaded into RAM, reducing I/O and memory waste.
Performance Overhead: Accessing data from disk (virtual memory) is much slower than from RAM. Excessive swapping leads to "thrashing," which significantly slows down the system4.
Hardware and Software Complexity: Requires support from both the OS and hardware (MMU).
Paging:
Virtual memory divides memory into fixed-size pages (virtual) and frames (physical). Pages are loaded into any available frames, allowing non-contiguous memory allocation. The page table maps virtual pages to physical frames, enabling efficient swapping between RAM and disk.
Segmentation:
Virtual memory divides memory into variable-sized segments based on logical divisions like code, data, or stack. Each segment can be independently loaded or swapped between disk and RAM. The segment table holds base and limit information for address translation.
Combined Approach (Segmented Paging):
Segments are further divided into pages. The virtual address includes a segment number and a page number. This combines the logical benefits of segmentation with the efficient memory use of paging, minimizing fragmentation and improving flexibility.