What Is Virtual Memory? - Codecademy Blog (2024)

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.

Learn something new for free

  • Intro to ChatGPT
  • Learn SQL

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.

FIFO

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.

LRU

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.

OPT

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.

Related courses

5 courses

Intro to ChatGPT Beginner Learn about ChatGPT, one of the most advanced AI systems available today, and dive into the world of Generative AI.
Learn SQL Beginner In this SQL course, you’ll learn how to manage large datasets and analyze real data using the standard data management language.
Learn to Code with Blockly Beginner Want to learn how to get started with programming in an interactive way? Try our drag and drop code lessons!
Choosing a Programming Language Beginner This course will introduce learners to factors to consider when picking a first language as well as describe some popular options.
Choosing a Career in Tech Beginner Are you feeling confused about where to aim for with your career goals? Take this course to learn about different career options!

Subscribe for news, tips, and more

Subscribe for news, tips, and more

What Is Virtual Memory? - Codecademy Blog (2024)

FAQs

What is the virtual memory short answer? ›

Virtual memory is a memory management technique where secondary memory can be used as if it were a part of the main memory. Virtual memory is a common technique used in a computer's operating system (OS).

What is virtual memory Quizlet? ›

Virtual memory (VM) VM is simulated memory that is implemented as a page file on a hard drive. Virtual memory is used by operating systems to simulate physical RAM using hard disk space. The process of moving data from RAM to disk (and back) is known as swapping or paging.

What should my virtual memory be set at 16GB RAM? ›

However, with advancements in technology and more efficient operating systems, this rule is not as rigid as it once was. For a system with 16GB RAM, setting the virtual memory between 24GB to 32GB is a good starting point, but adjustments might be needed based on specific use cases.

What should my virtual memory be set at 32GB RAM? ›

Note: Microsoft recommends that virtual memory be set at no less than 1.5 times and no more than 3 times the amount of RAM on the computer.

Top Articles
Latest Posts
Article information

Author: Mrs. Angelic Larkin

Last Updated:

Views: 5786

Rating: 4.7 / 5 (67 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Mrs. Angelic Larkin

Birthday: 1992-06-28

Address: Apt. 413 8275 Mueller Overpass, South Magnolia, IA 99527-6023

Phone: +6824704719725

Job: District Real-Estate Facilitator

Hobby: Letterboxing, Vacation, Poi, Homebrewing, Mountain biking, Slacklining, Cabaret

Introduction: My name is Mrs. Angelic Larkin, I am a cute, charming, funny, determined, inexpensive, joyous, cheerful person who loves writing and wants to share my knowledge and understanding with you.