How does virtual memory work?
Virtual memory is part of the hard disk and is used as an extension to the RAM during processing. It allows much larger operations to be carried out which are not normally possible using RAM alone, as it has limited space.
It is a very useful tool, used mainly for programs requiring much more memory space than the available RAM permits. This is achieved through the simulation of an infinite virtual space on HDD (even though virtual memory is still limited to a specific amount.)
The difference between RAM and hard disk
In order to have a clearer idea how virtual memory works, it is useful to take a closer look at two key elements in the memory of a personal computer.
The hard disk (or HDD) is the physical part of the memory. Its function inside the computer is to file and store data.
There are other quicker and more modern methods of transferring and writing data known as SSD (Solid State Drives) which, although using different data storage methods, basically carry out the same role.
RAM (Random Access Memory) also has the job of storing data, but for a limited time. Its main purpose in fact is not the storage of data, but to make it available as quickly as possible, in order to ensure the correct functioning of the processes underway.
Loading data is therefore quicker compared to when a hard disk is used, but the problem is that, as it involves volatile memory, when the PC is turned off, all the data present in the RAM is deleted.
The functioning of virtual memory in more detail
Now that we have considered the main elements of memory, we will take a closer look at how virtual memory works. When a process is very complex and sufficient space is not available inside the RAM, the operating system loads part of it into a special section of the hard disk, which works like an extension of the RAM.
This maintains the loading speed and allows easy access to all data relating to that process during its execution. In more precise terms, the hard disk (secondary memory) is used as provisional storage for data and processes which are not immediately necessary to the calculation being performed.
It is at this point that the swap comes into play. This term refers to an exchange of data/pages between the disk and RAM (swap-in) or between RAM and disk (swap-out). This operation, when required, enables pieces of the process previously stored in the virtual memory to be retrieved and used to replace the parts already operating in the RAM.
A good example of how swap and virtual memory interact is provided below:
- A process must be carried out;
- In order to do this as quickly as possible only the first pages are loaded into the RAM;
- The remaining pages are allocated to the virtual memory on the hard disk;
- As calculations involved in the process proceed, the required pages are retrieved from the virtual memory and substituted (swapped) for those present in the RAM which are no longer needed.
In order to find the right page to load into the RAM at any given time, the computer uses the so-called virtual address. This is a string composed of three elements: segment number, page number and offset. The segmentation and page layout are then added to identify precisely which other element is required by the process.
Translated by Joanne Beckwith
