Why Virtual Memory is Crucial Despite Having Real Memory ??

 Why Virtual Memory is Crucial Despite Having Real Memory ??

  •  In the early days of computing, programs directly used real memory, also known as RAM. This approach led to two major problems: external fragmentation and internal fragmentation.

Understanding Fragmentation:

  • Imagine memory as a large whiteboard where programs can write their instructions. Fragmentation occurs when this whiteboard gets divided into unusable chunks.
  • External Fragmentation: This happens when free memory becomes scattered across the whiteboard in small pockets. Even though the total free space might be enough for a new program, its non-contiguous nature makes it unusable. 

Internal Fragmentation:

  • This occurs when memory is allocated in fixed-size blocks. Suppose a program needs 101 KB, but the smallest allocation size is 128 KB. The remaining 27 KB becomes unusable, creating internal fragmentation.

Virtual Memory to the Rescue

  • To overcome these limitations, virtual memory was introduced. It acts as an intermediary layer between programs and physical RAM, creating a larger virtual address space. Here's how it works:

Virtual Memory Overview:

  • The operating system creates a virtual memory layer, essentially dividing RAM into equal-sized units called pages.
  • Virtual memory combines RAM and swap space (a designated section of your hard drive). When RAM fills up, less frequently used pages are swapped out to the hard drive, freeing up space for active programs.
  • This layer ensures processes don't overlap memory usage, preventing conflicts.
  • Virtual memory can be expanded beyond physical RAM by utilizing swap space.

Benefits of Virtual Memory:

  • Process Isolation: Virtual memory separates a process's virtual addresses from real physical addresses. This isolates processes, preventing one program from corrupting another's memory.
  • Increased Address Space: Virtual memory allows programs to use more memory than physically available, enabling the execution of larger programs or running multiple programs simultaneously.
  • Memory Protection: The operating system controls memory access, preventing unauthorized processes from reading or writing to protected areas.
  • Shared Memory: While processes are isolated, virtual memory allows controlled sharing of information between them for efficient communication.

Technical Details:

  • Paging: Imagine dividing the whiteboard (RAM) into equal-sized sections (pages) for efficient memory allocation.
  • Page Table: This data structure acts as a map, translating virtual page addresses used by a process into real page addresses in physical memory.
  • Demand Paging: Programs don't need all their memory pages loaded at once. Virtual memory loads pages only when needed, improving efficiency.
  • Page Faults: When a program tries to access a non-loaded page, a page fault occurs. The OS retrieves the required page from the swap space and updates the page table.
  • TLB (Translation Lookaside Buffer):  To speed up address translation, frequently accessed virtual-to-physical address mappings are cached in the TLB, reducing the need to constantly consult the page table.

In conclusion, virtual memory is a game-changer in memory management. It solves the problems of fragmentation, enables efficient multi-tasking, and provides a secure environment for running multiple programs. While RAM is the primary workspace, virtual memory acts as a powerful extension, allowing us to utilize our computers to their full potential.

Comments

All Post

Logging ??

Argo CD 101

What is GitOps in easy way ??

Why need of Cloud Computing ??

AWS and its Services ??

Build a Slack activity dashboard with Metabase

Prometheus Architecture...

Observability 101

Monitoring 101

Computer Networking 101