Memory Management Techniques: Swapping, Paging, and Virtual Memory
Memory Management Techniques
Main Memory: Necessary for operations to reduce the number of I/O operations to secondary storage.
OS Memory Management: Function: to keep the largest number of resident processes, enabling the maximization of resource sharing between processes and other computational tasks.
Strategies for Loading Programs into Main Memory
- Best-fit: The best partition is chosen, selecting a partition where the program leaves the smallest amount of unused space.
- Worst-fit: The worst partition is chosen, that is, the one where the program leaves the largest amount of unused space.
- First-fit: The first partition with enough free space to load the program is chosen.
Purpose: To prevent or reduce external fragmentation.
Swapping
Used to address main memory limitations.
How Swapping Works
Processes in memory are executed until they need to wait, then they are moved to secondary storage, freeing up space in main memory. When a process is removed from main memory, it is placed in a swap file on disk. Subsequently, this process can be loaded back into main memory and executed.
- Swap Out: The process of moving a process from main memory to a swap file on secondary storage.
- Swap In: The process of loading a process from a swap file on secondary storage into main memory.
Concepts Related to Swapping
- Swap File: A file on disk representing a stored process.
- Dynamic Allocation of Programs: A system implemented to allocate and deallocate programs (processes).
Paging and Page Replacement Policies
Virtual memory management allows the execution of a program without its entire code being resident in main memory. A mechanism is needed to manage virtual memory pages.
- Anticipatory Paging: Mapping all pages of a process from secondary to primary memory.
- Demand Paging: Mapping pages from secondary to main memory as they are needed for process execution.
Pages in memory can be allocated per process in a fixed or variable manner.
- Fixed: Each process has a fixed amount of allocated physical memory.
- Variable: Allocation varies according to available space.
Policies for Removing a Process from Main Memory
- Local Page Replacement Policy: Only pages of the process that generated the page fault are considered for replacement.
- Global Page Replacement Policy: All pages in main memory are considered for replacement, regardless of the process that generated the page fault.
- Page Fault: A situation where a process tries to access a page in main memory and it is not found, requiring the allocation of that page.
Virtual Memory Management
When there is not enough space in main memory to allocate new processes, virtual memory management uses a secondary storage device (HDD) to provide more space for process allocation. In multiprogramming systems, virtual memory management links the logical address used by the program to the physical address in main memory. The advantages of using virtual memory management are:
- Programs and their data structures are no longer limited by the physical size of available memory.
- Only part of each process needs to be resident in main memory, not everything.
- Allows a larger number of processes to share main memory.
Virtual memory management works with real addresses (in main memory) and virtual addresses (in virtual memory), using a mapping table to translate virtual addresses to real addresses. In virtual memory management, only part of a process resides in main memory, with the other part in virtual memory. A concept called “paging” is used by the operating system to work with various addresses. The pages in virtual memory are known as virtual pages, and the pages in main memory are known as real memory pages.
- Page Replacement Policy: Selection of which page should be released from main memory and replaced.
- Page Size: The page size is hardware-related and varies according to the process.
- Virtual Memory Segmentation: A memory management technique where the virtual address space is divided into blocks of different sizes.
- Segmentation with Paging: A technique where the address space is divided into segments, and each segment is further divided into pages.
- Swapping in Virtual Memory: Uses swap out, swap in, and swap files.
Memory Management Exercises
Storing and retrieving information are essential activities for all applications. Every process must read and write data on disk devices. To perform these activities, the operating system uses the “File System” to store, organize, and access files and directories.
File System Concepts
- What is a File? Logically related information.
- How are Files Organized? File organization consists of how data is stored internally.
- What are the Access Methods? Mechanisms for retrieving information from a file.
- Who Interfaces Between Applications and Devices? Input and output operations are routines of the file system.
- What are Directories? Root units that allow reading and writing of data.
- Disk Space Allocation Management: Concerns how files are stored on disk.