Memory Management - Virtual Memory
Memory Management - Virtual Memory
*Virtual Memory*: A memory management technique that allows a computer to use more
memory than is physically available by temporarily transferring pages of memory to disk
storage.
*Paged Memory Allocation*: Divides memory into fixed-size blocks called *pages*. Each
process is allocated a set of pages, and the operating system manages the mapping between
virtual addresses (used by the process) and physical addresses (in RAM or disk).
*Key Components*:
1. *Page Table*: A data structure that maps virtual pages to physical pages.
2. *Page Frame*: A physical block of memory that holds a page.
3. *Paging*: The process of transferring pages between RAM and disk.
*How it works*:
1. The operating system divides memory into pages (e.g., 4KB each).
2. Each process is allocated a set of pages, and the page table maps virtual addresses to
physical pages.
3. When a process accesses a page not in RAM, a *page fault* occurs.
4. The OS checks if the page is on disk and brings it into RAM (paging in).
5. If RAM is full, the OS removes an unused page from RAM and writes it to disk (paging out).
*Benefits*:
1. *Efficient memory use*: Allows more processes to run than physical memory would allow.
2. *Memory protection*: Each process has its own virtual address space, preventing memory
conflicts.
2} Demand Paging:
_Demand Paging_: A virtual memory technique that loads pages into RAM only when they are
actually needed (on demand).
_Key Points_:
1. _Page fault_: When a process accesses a page not in RAM, a page fault occurs.
2. _Page in_: The OS brings the required page from disk into RAM.
3. _Page out_: If RAM is full, the OS removes an unused page from RAM to make space.
4. _Lazy loading_: Pages are loaded only when needed, reducing memory usage.
_How it works_:
1. Process accesses a virtual address.
2. MMU (Memory Management Unit) checks if page is in RAM.
3. If not, a page fault occurs.
4. OS checks disk for the page.
5. If found, OS brings page into RAM (page in).
6. If RAM full, OS removes an unused page (page out).
_Benefits_:
_Page Replacement Algorithm_: A technique used by the operating system to select a page to
remove (replace) from RAM when a new page needs to be loaded and RAM is full.
_Common Algorithms_:
_FIFO Algorithm_: A simple page replacement algorithm that selects the oldest page in RAM to
replace when a new page needs to be loaded.
_How it works_:
_Example_:
_Advantages_:
- Simple to implement
- Low overhead
_Disadvantages_:
_LRU Algorithm_: A page replacement algorithm that selects the page that hasn't been
accessed for the longest time to replace when a new page needs to be loaded.
_How it works_:
_Example_:
_Advantages_:
_Disadvantages_:
_Implementations_:
- Hardware-based: Using a hardware counter to track timestamps
- Software-based: Using a software data structure (e.g., stack or queue) to track timestamps
_Key Points_:
1. _Segments_: Code, data, stack, and heap are separated into different segments.
2. _Segment Table_: A data structure that stores the base address and limit of each segment.
3. _Segmentation_: Each segment is allocated a contiguous block of memory.
_Benefits_:
_How it works_:
1. _Segmentation_: Divide memory into independent segments (code, data, stack, heap).
2. _Demand Paging_: Load pages into RAM only when needed (on demand).
_Key Points_:
_Benefits_:
6} Virtual Memory:
_Virtual Memory_: A memory management technique that allows a computer to use more
memory than is physically available by temporarily transferring pages of memory to disk
storage.
_Key Points_:
_Benefits_:
1. _More Memory_: Programs can use more memory than physical RAM.
2. _Efficient Memory Use_: Only needed pages are loaded into RAM.
3. _Memory Protection_: Programs are isolated from each other.
In summary, virtual memory allows a computer to use more memory than physical RAM by
transferring pages of memory to disk storage, providing efficient memory use, memory
protection, and allowing programs to use more memory than physical RAM.