Have you ever wondered how your computer can handle running a browser full of open tabs, email applications, the IDE with your latest development project, a virtual machine, and your music player — all at the same time? Sure, you have 8GB of RAM (random access memory) installed, but what happens when that runs out?
The truth is that we all use far more than our installed physical RAM and it’s all thanks to virtual memory. But what is virtual memory, and how does it work?
In this article, we’ll walk you through the basics of virtual memory, including what it is, how it works, and why it’s so important.
What is virtual memory?
Virtual memory is a memory management technique used by operating systems (OS). It allows a computer to temporarily increase the capacity of its main memory — RAM — by using secondary memory such as a hard drive or solid-state drive (SSD). Virtual memory utilizes hardware and software to manage how information is stored and retrieved from the hard drive.
Virtual memory helps people do more on their computers without paying for more RAM capacity. While memory is much cheaper today than it used to be, RAM is still one of the most expensive types of memory — only cache memory is more expensive.
How does virtual memory work?
Whenever your computer stores something in RAM, that information is assigned a memory address — a reference to where the information is stored on the RAM chip. When your computer starts running low on RAM, the OS will start transferring data from the RAM to your hard drive. The OS sets up a paging file: A dedicated space on your hard drive for virtual memory.
Data stored on a hard drive is always assigned a physical address — a reference to where the information is stored in the drive. The OS also maps physical addresses to virtual addresses as part of the virtual memory process. Virtual addresses look like RAM addresses. That way, when a program is running on your computer, it can seamlessly use RAM or virtual memory. When a program uses data stored in virtual memory, the OS knows how to find the data using the physical address.
To better understand how virtual memory works, let’s imagine a situation: calculating the sum of some numbers.
How virtual memory works
Suppose that you need to sum up a list of numbers without using a calculator. If someone just gave you three or four small numbers, chances are that you could calculate the sum fairly quickly.
Now imagine that, instead of three small numbers, you needed to calculate a long list of big numbers, small numbers, negative numbers, and decimals. To help make sure you don’t forget anything, you might write down the list of numbers on a sheet of paper. Then, you could sum up the first two numbers, remember the sum, add the third number to the sum, and so on. In this example, the numbers you can remember correspond to the RAM, while writing down numbers is similar to using disk storage.
We all have different capacities to quickly recall complex information that we just learned — in other words, we all have different RAM capacities. By putting some information into a form of long-term storage — on a sheet of paper — we can focus our mental efforts on solving the problem one step at a time.
Of course, writing down everything takes time. It’s not as fast as memorizing each step and keeping everything in your head. That’s the trade-off of virtual memory. While it does help improve computer performance and multitasking, virtual memory is slower than RAM.
The OS constantly decides which data should be quickly accessible and keeps that data in the RAM to optimize performance. If the data isn’t needed right away, then the OS moves it to virtual memory.
How does your computer decide what goes into virtual memory?
Once your computer’s RAM is full, the OS will need to start moving data to virtual memory. The next question is much trickier: how should a computer decide what stays in the faster RAM and what goes to slower virtual memory?
Three common algorithms are used for managing virtual memory: FIFO, LRU, and OPT.
First-in, first-out (FIFO) is the most straightforward virtual memory management algorithm. The idea is to take the data that’s been in RAM the longest and move it to virtual memory. When that space is used up, the data that’s been in RAM the second-longest is moved to virtual memory, and so on.
It’s easy for an OS to use FIFO since it doesn’t rely on complex labeling and prediction algorithms. On the other hand, it’s rarely the most practical algorithm to use — just because your computer loaded some data into RAM a while ago, doesn’t mean it’s not in use.
A more sophisticated virtual memory algorithm is called the least recently used (LRU) algorithm. Here, your computer keeps track of when data is used in RAM. When it’s time to make more room in RAM, the computer replaces the data that hasn’t been used for the longest time.
The most advanced virtual memory algorithm is known as the optimal algorithm, or OPT. In addition to considering past data usage like LRU, OPT also predicts which data will be needed soon. That way, the most relevant data is always kept in RAM.
While the OPT algorithm offers the best virtual memory performance, it’s also the hardest to implement.
Why not use virtual memory all the time?
Virtual memory is an amazing tool that helps people get more out of limited memory resources on their computers, so why not use it all the time — or even dedicate an entire hard drive for virtual memory? There are two big reasons that virtual memory shouldn’t be the default substitute for RAM.
First, it takes more time to read and write data on a hard drive than it does on RAM. The more your computer relies on virtual memory, the slower your programs will run and the less you’ll be able to effectively run multiple programs at once.
Second, swapping data between virtual memory and RAM takes time. If the OS spends too much time swapping data between RAM and virtual memory, it can lead to thrashing — a severe drop in performance because too many computer resources are being dedicated to managing virtual memory and updating the paging file.
Virtual memory is important for every developer
Without virtual memory, your computer would stop loading more applications once the RAM was full. It would be up to you to close your browser tabs, shut down applications, and manage RAM usage on your own. You might not even be able to run some applications at all without upgrading your RAM.
Software Developers can focus on stunning front-end designs and optimized back-end solutions to create amazing web pages and applications thanks to virtual memory. Some programming languages like C++ even allow you to manage memory resources directly.Interested in learning more? Our online programming courses will teach you more about C++ and other in-demand programming languages.