Random Access Memory Ram

Where is Virtual Memory stored on a Linux system?

On a Linux System, the virtual memory is stored in the SWAP File system.

This was the quick answer to the Quizlet-style question. Keep reading If you want to know more about Virtual Memory, how it works, and how to manage it.

Table of Contents

What is virtual memory?

Every computer we use needs to have available memory to work properly, but there are limitations to how much physical memory can be put into a system. The workaround that allows you to use programs that go beyond the size of that limited physical memory relies on virtual memory.

Imagine that you own a bookstore. A customer calls and wants to purchase a book. You check the stock and find that it’s not one that is frequently purchased so, due to limited shelf space, it’s kept in a warehouse rather than the actual store.

In this situation, you wouldn’t tell the customer they can’t have the book. Instead, you would retrieve the book from the warehouse so it would be available when they arrived, bringing the book to them without requiring any additional work on the customer’s part. In fact, the customer would be unlikely to even realize that the book was being kept in the warehouse in the first place.

Virtual Memory Physical Memory Schema
Ehamberg, CC BY-SA 3.0, via Wikimedia Commons

This same principle can be applied to virtual memory. Just as you keep the most popular books stocked in the store, your operating system (OS) has to prioritize the processes that it keeps readily available by the frequency of use. However, that doesn’t mean you can’t run other processes. It simply means that your system has to access swap space, the metaphorical warehouse, to run them.

History of Virtual Memory

Virtual memory has changed over time, but it has a long history of use, going all the way back to the 1950s and the development of the Atlas Computer, which is often considered the world’s first supercomputer.

What is the difference between virtual and physical memory?

Although physical memory and virtual memory are both called memory, there are still important differences between them.

Physical memory is made up of two concrete locations: random access memory (RAM) and disk space. The primary memory, RAM, is attached to the motherboard, while secondary memory
is a form of mass storage, such as a hard disk drive.

In contrast, virtual memory isn’t storage space in a touchable, concrete form. Instead, it’s a technique that the system can use to give the illusion that more memory exists.

Going back to the bookstore analogy, physical memory would be the shelf space in the store and the warehouse where the less common books are safely kept until they are needed. Virtual memory would be the software the customer uses to order the book. The software would also have a table to tell the bookstore employee where to find each book.

Why do I need virtual memory?

Every system has a limited amount of RAM, which is short-term memory where data is stored as the processor needs it. For example, when you run a program, the system writes and retrieves data from the RAM. In times when the RAM is full, the data still needs somewhere to go so that your device can continue to function. Virtual memory allows operating systems to load programs that are larger than the available physical memory.

Random Access Memory Ram
A computer’s RAM module

Pros and Cons of Virtual Memory

There are several advantages and disadvantages to using virtual memory.

Pros

Using virtual memory allows you to

  • Run more applications at once.
  • Develop programs without the concern of managing a shared memory space. In this way, every process can have its own virtual address space to manage its memory.
  • Provides memory isolation, as every Process has its virtual address space and can’t access other precess’s space.
  • Use several large programs despite the limited RAM space.
  • Gain speed in situations where only a specific segment of the program is necessary for program execution.
  • Create a multiprogramming environment with larger applications without the limitations of RAM space.

Cons

On the other hand, using virtual memory can result in

  • Slower applications
  • Extended time switching between applications
  • Reduced system stability
  • Negatively affected overall performance

How does virtual memory work?

When running an application, a physical address in the RAM is used to store the data. When devices attempt to run processes while the RAM is full, the memory management unit creates the necessary virtual addresses for the data. The processor then converts each virtual address into a real address based on information held in a table or set of tables maintained by the OS.

How does the Linux kernel manage the process’s memory?

The Linux kernel manages the process’s memory by creating contiguous virtual address spaces. This enables the process to access the virtual memory.

Also, the Linux kernel’s processes use the virtual memory manager, so it’s not only used for user processes but for system processes too.

Types of virtual memory management: paging and segmentation

There are two types of management, each type has its own different characteristics.

  • Paging: In this form of memory management, the process address space is broken into pieces called pages. They are fixed in size based on the available memory.
  • Segmentation: The other option for virtual memory management, segmentation, breaks the process address space into blocks called sections. These vary in size based on user input.

Nearly all systems these days use the technique of paging, with a page segment table to translate virtual storage addresses into real memory ones. The work of translating virtual to real addresses is handled by the memory management unit. 

What filesystem does Linux use to store virtual memory?

Linux uses tmpfs to keep data in the system in virtual memory.

Where is the virtual memory stored?

Virtual memory has two physical locations: RAM and disk swap. The part of the virtual memory that is on the hard disk is referred to as swap space, and it is used when the RAM is full or the system is idling. With swap space, a program that has not been used in a while can be moved to the swap file while the current program is switched over to the RAM.

The paging supervisor is in charge of managing the transfer of pages from swap to RAM and from RAM to swap. It uses an algorithm to decide which page should be moved from RAM to secondary storage (Known as Swap out operation).

Is virtual memory always used?

Virtual memory is specifically intended for situations where the RAM is too small, but many modern systems always use it regardless of whether there is enough RAM or not.

Is there an increase in performance with virtual memory?

Virtual memory tends to have a negative effect on performance intended as speed.

On one hand, it allows you to use larger programs and multiprogramming. On the other hand, memory on the hard disk will always be significantly slower than the RAM, so if something needs to be retrieved from the swap space, the disk operations will slow down the overall performance.

Virtual Memory Management

It’s possible to make changes to your available virtual memory, but there are some things to consider before you do.

What command can I use to check memory usage in Linux?

To check the amount of available memory, you can use the command use the command

# free -h
Linux free -h Output

The first line of the output represents the RAM usage, while the second represents the SWAP space used.

Should I increase virtual memory on my system?

If your system has gotten sluggish or unresponsive, it’s possible that both the RAM and swap disk space are nearly full and that you need to create more.

Is there a maximum size for virtual memory?

Yes, there is a limit to the virtual memory size for your system. This is because frequently swapping data, a process called thrashing, can cause the system to slow and become unstable. Consider the amount of RAM before adding more swap space. If you have a smaller amount of RAM, you may want to have no more than double the amount of swap space. If your device has a large amount of RAM, don’t be tempted to create a matching amount of swap as it is likely to overwhelm the system.

How do I increase virtual memory in a Linux system?

The easiest way to get additional memory in the system is by increasing the RAM, but it’s also possible to create a new swap file or partition in the system.

Why is Linux swapping with free memory?

Linux will swap processes that haven’t been used recently, basically moving them out of the way, especially if your RAM is nearly full, so that current processes have enough space to function correctly.

How to free up memory in Linux?

One way to free up memory in Linux is to clear the cache.

To clear only the PageCache, use

sudo sh -c 'echo 1 > /proc/sys/vm/drop_caches'
Linux Clear Page Cache

To clear the entries and nodes, use

sudo sh -c 'echo 2 > /proc/sys/vm/drop_caches'
Linux Clear Entries Nodes Output