Skip to content
Course Content
Heap Common Core

Physical Memory

Physical memory refers to the actual RAM (Random Access Memory) chips installed in a computer. This is the tangible hardware that stores data temporarily while the system is running. Physical memory is a finite resource determined by the amount of RAM installed in the machine.

Each byte of physical memory has a unique physical address that the memory controller uses to access the data. However, applications do not directly interact with physical addresses in modern operating systems.

Virtual Memory

Virtual memory is an abstraction layer provided by the operating system that gives each process the illusion of having its own private, contiguous address space. This address space is much larger than the available physical memory.

On 32-bit Windows systems, each process can address up to 4GB of virtual memory (with 2GB typically available to user mode by default). On 64-bit systems, the theoretical limit is 16 exabytes, though practical limits are much lower (typically 8TB on Windows).

The Translation Process

Virtual addresses used by applications are translated to physical addresses by the Memory Management Unit (MMU) in the CPU. This translation happens transparently and is managed through page tables maintained by the operating system.

The translation occurs in units called pages. On x86 and x64 systems, the standard page size is 4KB (0x1000 bytes). When a process accesses a virtual address, the MMU looks up the corresponding physical page in the page table.

Benefits of Virtual Memory

Virtual memory provides several critical benefits:

  • Isolation: Each process has its own address space, preventing one process from accidentally or maliciously accessing another process’s memory.
  • Abstraction: Applications can use simple, contiguous addresses without worrying about where data is physically located in RAM.
  • Overcommitment: The system can allocate more virtual memory than physical memory available, using disk paging to supplement RAM.
  • Memory protection: Pages can have different access permissions (read, write, execute), enforced by the CPU.

Virtual Memory States

In Windows, virtual memory pages exist in one of three states:

  • Free: The page is not allocated and cannot be accessed. Any attempt to access it will cause an access violation.
  • Reserved: The address range is set aside for future use but has no physical storage backing it. Accessing reserved pages also causes an access violation.
  • Committed: The page has physical storage allocated to it, either in RAM or in the page file. The page can be safely accessed.

Why This Matters for Heaps

The Windows heap manager operates on top of virtual memory. When you allocate memory from a heap, you are receiving virtual addresses. The heap manager calls VirtualAlloc() to obtain large regions of virtual memory and then subdivides these regions into smaller allocations for your application.

Understanding the distinction between physical and virtual memory is crucial because:

  • Virtual memory operations (like VirtualAlloc) are relatively expensive system calls.
  • The heap amortizes the cost of VirtualAlloc by managing a pool of virtual memory and serving many small allocations from it.
  • Not all virtual memory consumes physical RAM until it is actually accessed (lazy commitment).