0% found this document useful (0 votes)
13 views

Memory Management - Virtual Memory

Memory management (operating systems) complete notes

Uploaded by

misbasaiyed075
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Memory Management - Virtual Memory

Memory management (operating systems) complete notes

Uploaded by

misbasaiyed075
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Memory management: virtual memory

1} Virtual Memory (Paged Memory Allocation):

*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_:

1. _Efficient memory use_: Only needed pages are loaded.


2. _Reduced memory waste_: Unused pages are not loaded.
3. _Improved performance_: Pages are loaded only when needed.

3} Page Replacement Algorithm:

_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.

_Goal_: Minimize page faults and optimize memory usage.

_Common Algorithms_:

1. *First-In-First-Out (FIFO)*: Replace the oldest page in RAM.


2. *Least Recently Used (LRU)*: Replace the page that hasn't been accessed for the longest
time.
3. *Optimal*: Replace the page that won't be needed for the longest time (ideal but difficult to
implement).
4. *Clock*: A variation of LRU, uses a circular buffer to track page usage.

1...First-In-First-Out (FIFO) Page Replacement Algorithm:

_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_:

1. Pages are loaded into RAM in a queue (FIFO order).


2. When a page fault occurs and RAM is full:
- The oldest page (first loaded) is selected for replacement.
- The selected page is removed from RAM and written to disk (if modified).
- The new page is loaded into RAM at the end of the queue.

_Example_:

RAM: [Page 1, Page 2, Page 3] (loaded in this order)

Page fault: Page 4 needs to be loaded


- Page 1 (oldest) is replaced with Page 4
- RAM: [Page 2, Page 3, Page 4]

_Advantages_:

- Simple to implement
- Low overhead

_Disadvantages_:

- Can lead to thrashing (frequent page faults)


- Doesn't consider page usage patterns

2...Least Recently Used (LRU) Page Replacement Algorithm:

_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_:

1. Each page in RAM is assigned a timestamp of when it was last accessed.


2. When a page fault occurs and RAM is full:
- The page with the oldest timestamp (least recently used) is selected for replacement.
- The selected page is removed from RAM and written to disk (if modified).
- The new page is loaded into RAM with a new timestamp.

_Example_:

RAM: [Page 1 (t=10), Page 2 (t=20), Page 3 (t=30)]

Page fault: Page 4 needs to be loaded

- Page 1 (least recently used) is replaced with Page 4


- RAM: [Page 2 (t=20), Page 3 (t=30), Page 4 (t=40)]

_Advantages_:

- More efficient than FIFO


- Reduces thrashing
- Considers page usage patterns

_Disadvantages_:

- More complex to implement


- Requires additional hardware or software to track timestamps

_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

4} Segmented Memory Allocation:

_Segmented Memory Allocation_: A memory management technique that divides a program's


memory into smaller, independent segments, each with its own base address and limit.

_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_:

1. _Memory Protection_: Segments can have different access rights (read/write/execute).


2. _Efficient Memory Use_: Segments can be allocated and deallocated independently.
3. _Reduced Fragmentation_: Segments can be compacted to reduce memory waste.

_How it works_:

1. Program is divided into segments (code, data, stack, heap).


2. Segment table is created with base address and limit for each segment.
3. Memory is allocated for each segment.
4. Segment table is used to translate virtual addresses to physical addresses.

5} Segmented/Demand Paged Memory Allocation:

_A combination of Segmented and Demand Paged Memory Allocation_:

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_:

1. _Segment Table_: Stores base address and limit of each segment.


2. _Page Table_: Maps virtual pages to physical pages.
3. _Paging_: Loads pages into RAM only when accessed.
4. _Segmentation_: Provides memory protection and efficient memory use.

_Benefits_:

1. _Memory Protection_: Segments have different access rights.


2. _Efficient Memory Use_: Segments and pages are allocated and deallocated dynamically.
3. _Reduced Memory Waste_: Pages are loaded only when needed.
_How it works_:

1. Program is divided into segments (code, data, stack, heap).


2. Segment table is created with base address and limit for each segment.
3. Pages are allocated to segments as needed (demand paging).
4. Page table maps virtual pages to physical pages.
5. Memory is allocated and deallocated dynamically.

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_:

1. _Virtual Address Space_: A program's memory is divided into virtual pages.


2. _Physical Memory (RAM)_ : Pages are loaded into RAM as needed.
3. _Paging_: Pages are transferred between RAM and disk storage.
4. _Page Table_: Maps virtual pages to physical pages.

_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.

~Credits to misba saiyed.

You might also like