Virtual Memory

Motivation

Direct access to RAM comes with serious risks:

Security Risks

  • No isolation between processes: One process could read or modify another process’s memory.

  • Malware injection: Malicious code could alter OS data structures or inject code into other processes.

  • Bypass protections: Without virtual memory, access control (read/write/execute) for memory regions becomes very hard to enforce.

Stability Risks

  • Crashes and corruption: A bug in one program could overwrite memory used by the OS or another app, causing crashes or data loss.

  • Unpredictable behavior: Without virtual memory, address conflicts can occur between different apps or system code.

Virtual Address Space

  • Virtual Address Space is the range of memory addresses that a process can use.

  • For example, two programs might both use address 0x1000, but they map to different physical locations in RAM.

  • In a 32-bit system, the maximum virtual address space is:

232 bytes=4 GB 2^{32} ~\text{bytes} = 4 ~\text{GB}
  • Physical Memory (RAM) is typically smaller than Virtual Memory:

    • Virtual memory is a concept provided by the operating system that allows programs to use a larger logical address space than what is physically available in the system’s RAM.

    • Physical memory refers to the actual RAM installed on the system, which is finite (for example, 8 GB or 16 GB of RAM).

  • Processes typically do not use the entire virtual address space.

  • Virtual memory allows programs to have access to a larger address space than the physical RAM installed by paging and swapping data to and from the hard drive or SSD when needed.

  • Operating systems use this technique to give the illusion that the system has more memory than it actually does.

Page

  • A Page is a contiguous block of memory with a fixed size, and it’s the smallest unit of data that can be transferred between virtual memory and physical memory.

  • The operating system divides both physical memory (RAM) and virtual memory into pages of the same size to manage them efficiently.

  • The size of a page determines the minimum size of memory allocations that can be managed by the operating system or hardware.

  • Pages are referred to as Tiles in some contexts:

    • Tile is sometimes used synonymously with page, especially when referring to memory management in GPU (Graphics Processing Unit) systems.

    • In GPUs, tiles are used to describe sections of memory for efficient parallel processing, though the concept is still based on splitting memory into manageable blocks.

Typical Page Size

  • 4 KB (Kilobytes) is the most common page size for 32-bit systems and is still widely used in 64-bit systems.

  • Larger pages (2 MB, 1 GB) are also used for performance optimizations in 64-bit systems.

Fragmentation

  • Allocating memory in large blocks (pages) can lead to internal fragmentation if the allocated space is not fully utilized (e.g., if only a few bytes of the 4 KB page are used).

  • Even if a process only needs, say, 1 byte of memory, it will still be allocated 1 full page (e.g., 4 KB in size on most systems), thus 4,095 unused bytes are wasted.

  • Fragmentation is limited to single pages.

Multiple Separate Virtual Address Spaces

  • Each process is given its own private virtual address space that is logically independent from others.

  • Memory Protection and Process Isolation

Memory Protection

Certain memory areas should be protected:

  • Application code should not be modified.

  • Processes should not access the OS code and data structures.

Memory Management Unit (MMU)

  • The Memory Management Unit (MMU) is a hardware component responsible for managing memory access.

  • It translates virtual addresses to physical addresses (Virtual Address Translation).

  • It ensures memory protection, isolation, and efficient use of memory through techniques like paging and segmentation.

Page Table

  • The operating system (OS) manages the mapping of virtual addresses to physical addresses through the Page Table.

  • The Page Table ensures that when a process accesses memory, the Memory Management Unit (MMU) can look up the correct physical address corresponding to the virtual address.

  • Page Tables are stored in physical memory (RAM).

Relocation Problem

  • The relocation problem refers to the challenge of ensuring a program can run correctly regardless of where it is loaded in memory.

  • Since programs may not always be loaded at the same physical address, absolute addresses in the code must be adjusted (or “relocated”) at load time or managed through virtual memory, so the program can function properly no matter where it resides.