OS Memory Management Notes
OS Memory Management Notes
Virtual Memory
Virtual memory is a memory management technique in an operating system (OS) that creates the
illusion of a large, continuous block of memory for applications, even if the physical RAM (Random
Access Memory) is limited. It allows the OS to use a combination of physical memory (RAM) and
disk storage to simulate a larger amount of memory, enabling programs that require more memory
1. Paging: The OS divides memory into fixed-size blocks called pages. It can load only the required
pages into RAM and store the rest on the disk, improving memory utilization.
2. Page Table: A table that maps virtual addresses to physical addresses. The OS uses this table to
3. Swap Space: When the RAM is full, the OS can move some inactive pages to a designated area
on the disk, called swap space, to free up RAM for active processes.
4. Demand Paging: The OS loads pages into memory only when they are needed, which reduces
- Efficient Memory Utilization: Allows the execution of large programs or multiple applications
- Process Isolation: Each process gets its own virtual memory space, which enhances security and
stability.
- Simplified Memory Allocation: Reduces the need for complex memory management in
applications.
Valid-Invalid Bit Scheme
The valid-invalid bit scheme is a memory management mechanism used in operating systems to
keep track of which pages are loaded in memory (RAM) and are valid (accessible) or invalid (not
currently in memory or inaccessible). It is often part of the page table, which maps virtual addresses
- Valid Bit: When a page is marked as valid (1), it means that the page is currently loaded in physical
- Invalid Bit: When a page is marked as invalid (0), it means that the page is not currently in physical
1. Memory Access Check: When a process tries to access a page, the OS checks the page table
entry for that page. The valid-invalid bit indicates if the page is in memory.
2. Page Fault Handling: If the bit is invalid (0), the OS triggers a page fault.
3. Security and Stability: The valid-invalid scheme prevents processes from accessing memory that
Benefits
- Efficient Memory Usage: The OS loads only pages that are actively being used, reducing memory
requirements.
- Improved Performance: By keeping track of valid and invalid pages, the OS avoids unnecessary
memory accesses.
- Process Isolation: Ensures that each process can only access its allocated memory.
Page Fault Handling
Page fault handling is the process by which an operating system (OS) manages a situation when a
program tries to access a page that is not currently loaded into physical memory (RAM).
1. Page Fault Detection: The OS detects a page fault when a process accesses a page marked as
invalid.
2. Suspend the Process: The CPU traps to the OS (enters the OS code) and suspends the process
3. Determine the Type of Page Fault: The OS determines whether the fault is legitimate (page not in
4. Locate the Required Page on Disk: The OS finds the page in secondary storage (disk) in a
5. Allocate a Free Frame in Physical Memory: If no free frame is available, it applies a page
6. Load the Page into Memory: The OS reads the page from disk into RAM and updates the page
table.
7. Resume the Process: The OS restarts the process at the point where it was interrupted.
If no free frames are available, a page replacement algorithm decides which page to remove from
memory.
Performance Impact
Page faults are costly, as disk access is slower than RAM access. Frequent page faults can lead to
thrashing.
Page Replacement Algorithms
Page replacement algorithms are used by operating systems to manage memory when handling
page faults. When a page fault occurs, these algorithms decide which page to remove to make
room.
1. First-In-First-Out (FIFO): Replaces the page that has been in memory the longest.
2. Optimal Page Replacement (OPT): Replaces the page that will not be needed for the longest
3. Least Recently Used (LRU): Replaces the page that has not been used for the longest time.
4. Least Frequently Used (LFU): Replaces the page that has been used the least frequently.
5. Most Recently Used (MRU): Replaces the page that was most recently used.
6. Clock (Second-Chance) Algorithm: Uses a circular buffer to give each page a second chance
before replacement.
Fragmentation
Fragmentation in an operating system refers to inefficient memory usage caused by scattered free
1. External Fragmentation: When free memory is divided into small, non-contiguous blocks. Caused
2. Internal Fragmentation: Wasted space within allocated blocks due to fixed-size allocations.
Solution: smaller allocation units or segmentation.
- Internal Fragmentation: Caused by fixed-size allocations, wastes space within blocks. Solution:
Fragmentation Management
Operating systems use paging, segmentation, compaction, and memory pooling to minimize
fragmentation.