What is Virtual Memory and how does it work?

Affiliate Disclosure: This post may include affiliate links. If you click and make a purchase, I may earn a small commission at no extra cost to you.

Imagine you are studying at a small desk with books and notes on it. The desk represents the limited but fast RAM, while the bed or closet represents the larger but slower storage. Since all your books and notes cannot fit on the desk at once, you keep the most important ones on the desk and place the rest on the bed. Whenever you need a book from the bed, you swap one from the desk to make space and bring the needed one over. This gives the illusion that you have a much larger desk than you actually do, just as virtual memory makes a program believe it has more continuous memory than the physical RAM available.

On any modern computer, the CPU and operating system always use virtual memory, even when there’s plenty of RAM available. Every memory access (loading a variable, accessing an array, etc.) goes through the virtual memory system because the CPU translates virtual addresses to physical addresses via the MMU (Memory Management Unit). And this happens a billion times per second. Essentially, Virtual memory offers its benefits through address translation, and mechanisms such as paging (and sometimes swapping) are employed to implement it.

However, swapping (when RAM is full) is the common understanding of virtual memory. The “classic” idea of virtual memory, which uses disk (pagefile/swap space) as an extension of RAM, only occurs when the system runs low on physical memory and programs are demanding for more RAM. However, virtual memory is integrated much deeper into the system design and operating systems. We will cover the concept of virtual memory in detail, along with paging and swapping. So, let’s get started.

The Need for Virtual Memory

Virtual memory addresses three primary issues: insufficient memory, memory fragmentation, and security concerns. In fact, virtual memory is how modern computers provide programs with access to the physical RAM. It is a software layer between the physical memory (RAM) and the CPU. The operating system is responsible for the execution.

In the past, computer memory was expensive, and systems had significantly less memory than we have today. At that time, virtual memory enabled programs to utilize more memory than is available in RAM by transparently moving data to/from disk (paging). For example, a PC with 4 GB RAM can run applications that collectively allocate more than 4 GB, because unused pages can be swapped to disk.

Now, let’s come to the next problem, which is memory fragmentation. If we offer memory space to programs directly from physical memory, there is a chance that the memory will become fragmented. Programs require contiguous memory allocation primarily for efficiency in address calculation. For example, we have 4GB of RAM, and we allow two programs, each with 1GB of RAM; they will work fine. However, if this space is allocated separately from the total capacity, there is a chance that the remaining 2GB will not be a single fragment. We can still allow a program to use 2GB from two different contiguous locations, but this will cause issues like cache misses and slow performance. Virtual memory allows each process to view a continuous address space, even if physical RAM is fragmented. The OS remaps pages behind the scenes.

Malicious programs could read/write arbitrary memory. Virtual memory enforces read/write permissions on pages. It also prevents code injection and unauthorized access processes.

Virtual memory also addresses various other problems and offers benefits such as shared libraries, memory separation for multitasking, and simplified programming.

Working of memory with the CPU

When a processor runs code, it doesn’t work with physical memory addresses directly. Instead, it deals with virtual addresses, whether it’s fetching instructions, reading data, or writing results. These virtual addresses are passed to the Memory Management Unit (MMU), which translates them into physical locations in RAM using page tables managed by the operating system. The operation of DRAM with the CPU is more complex at the basic level, but I have already published an article on DRAM, which you can check here.

To avoid performance penalties, the CPU utilizes a Translation Lookaside Buffer (TLB) to cache recent translations, allowing most memory accesses to be resolved instantly. If the requested virtual page is not currently mapped to RAM (resulting in a page fault), the OS intervenes, loading the required data from disk into memory and updating the page tables. To the running program, this entire process is invisible. It sees an ample, continuous memory space, while the CPU and OS handle the actual mapping behind the scenes.

The Basics of Virtual Memory

Virtual Memory solves a key problem: the programs have access to the same memory space. It is a perfect solution because it does the jobs of memory abstraction, memory isolation, and memory space enhancement (artificially). When you run a program, the operating system creates a virtual address space for that process. To the program, it appears to own a vast, private block of memory (e.g., 4 GB on a 32-bit system, or terabytes on a 64-bit system).

Behind the scenes, the OS + MMU (Memory Management Unit) maps those virtual addresses to actual physical memory locations.

It gives the programs the illusion of having a large, continuous block of memory, even if the physical RAM is smaller or fragmented in size. If the required data is not currently in RAM, the operating system uses paging and, if needed, swapping to bring it from secondary storage.

How it Works – Step by Step 

Let’s start from the very basics. Virtual memory is mainly the job of the operating system; however, it is controlled and executed by the CPU. So, the main thing is to provide the virtual memory addresses to the programs. This makes every program feel that it has its own continuous memory location where it can store all the essential data. A simple illustration to help you understand it better.

The CPU generates the virtual addresses every time a new program accesses the memory. However, these addresses aren’t used directly in the RAM. In short, a virtual address is a “fake” address that programs use. But when a program uses a virtual address, it should be translated into a physical address so that the actual RAM location can be provided to it. This is all done by the MMU or Memory Management Unit. The MMU is built into the CPU itself.

When the CPU (via the MMU) translates a virtual address to a physical address, it needs to know where the page currently resides. When a program tries to access a virtual address, the CPU checks the page table to see if that page is already in RAM. If it’s available, the program gets direct access; otherwise, a page fault occurs, and the OS fetches it from the disk. A virtual address has two parts.

  • Page number (high-order bits) → identifies which virtual page it belongs to.
  • Page offset (low-order bits) → the exact byte inside that page.

The OS maintains a page table that maps each virtual page number to a physical frame number (if loaded). The MMU checks the page table entry to confirm.

Now, there can be two possible outcomes whenever we run a program. The virtual address can already be in the RAM, which means the program gets direct access and the operation is faster. However, in other cases

Your computer’s RAM is broken up into pieces called pages. When you run out of RAM, less frequently used pages are removed from the RAM and stored on the hard disk, a process known as “swapping” (this area is called “swap space”). When the pages are needed again, they are swapped back into RAM.

If an application requests data from one of the pages that was swapped to disk, the system reports what is called a page fault. Then, the operating system retrieves the content for the requested page from the hard disk and loads it back into RAM. This is slower than accessing the RAM directly, but it would prevent a crash of your computer and your application.

Think of RAM as your desk space and disk as your bookshelf:

  • If a book (or page) you need is on your desk, you have instant access.
  • If it’s on the bookshelf → you have to stand up, fetch it, and maybe put another book back → slow (page fault).
  • To mitigate this, you keep the books you’re currently using on your desk (locality + replacement algorithms).

Benefits of Virtual Memory

Programs can use more memory than the physical RAM installed, since parts of memory can be swapped to disk. This allows very large applications to run on limited hardware.

Each program gets its own private virtual address space. One program cannot accidentally overwrite another program’s memory or the operating system’s memory.

Virtual memory simplifies programming for everyone. Programmers don’t need to worry about where exactly in RAM their program will be loaded. Every process sees a clean, continuous block of memory, even if the real RAM is fragmented. Virtual memory also facilitates efficient RAM usage by retaining only the parts of programs that are actively in use. Unused portions can remain on the disk until needed.

Each program has its own virtual memor,y and this allows for better multitasking. All in all, virtual memory makes systems more flexible, efficient, and secure by providing bigger memory space, process isolation, simplified programming, efficient RAM usage, and smooth multitasking.

Know the Things You Will Sacrifice.

Of course, nothing is great; everything comes with a cost, and with virtual memory, there is an opportunity cost:

Page faults are expensive in terms of performance because disk access is way slower than RAM. However, something is better than nothing, and without virtual memory or swapping, the last resort for a program without enough RAM will be to crash.

For large virtual address spaces (especially 64-bit), page tables can be huge. A 64-bit system with 4 KB pages could theoretically require petabytes of page tables, so multilevel paging is needed → more complexity.

To use virtual memory, the OS and hardware must coordinate (page tables, TLB, interrupts). Managing page replacement, working sets, and thrashing control is a non-trivial task.

A Real-Life Example

Picture this: you’re editing a video on your laptop. You have an 8 GB project, but only 4 GB of RAM. Without virtual memory, your laptop would freeze up. Thanks to virtual memory, the system can keep the essential parts of your project in RAM while moving the less critical bits to swap space.

It may not perform as if you have 16 GB of RAM, but it definitely gets the job done.

Virtual Memory versus RAM

FeatureRAM (Physical Memory)Virtual Memory (Swap Space)
SpeedVery fast (nanoseconds)Much slower (milliseconds, depends on disk/SSD)
Storage MediumSemiconductor chips on the motherboardHard drive or SSD storage
CapacityLimited and expensive per GBMuch larger and cheaper per GB
PurposeHolds data and instructions actively in useExtends RAM by temporarily storing inactive data
AccessDirectly accessed by the CPURequires MMU translation + possible disk access

In a nutshell 

Whenever you look at virtual memory, you can conceptualize it as the eiderdown of modern computing. It is the magic that allows you to perform many tasks simultaneously (much like a small desk relaxing an entire library of tomes) and allows your computer to execute more applications than its physical memory size would otherwise dictate. 

The next time your laptop is slowing down, you will know that it is not being lazy, it is actually busy swapping data between its RAM, and this must be an enormous job considering the amount of data it may have to deal with in most circumstances. It will be indexing all of this against what it has stored in its virtual memory.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments