Articles

What is Virtual Memory in Computers? – Learn Here

What is virtual memory in computers and how do you make use of it?

In the most basics of definition, virtual memory is the characteristic of the operating system that allows the hard disk to be used as temporary RAM.

Let us try to understand this concept a bit deeper:

What should you do if the computer memory is insufficient to run multiple programs simultaneously on the computer?

What is Virtual Memory

You will get to know that the memory is insufficient when you are shown a message by the operating system, saying that you must close some programs if you wish to run a particular program.

Which in other words means, that you are running low on memory.

How to Resolve the Issue of Low Memory?

One way to resolve this issue, as the message says, is to close some of the running programs and run your desired program again.

But that’s not always something desirable. You may not necessarily always have multiple programs at a time. Sometimes the reason of insufficient memory could well be the fact that you are working on some professional applications that are eating up your memory.

Another way, therefore, is to increase that RAM in the system. However, not everybody wants to dish out extra bucks for RAM.

Finally, the easiest, immediate and the cheapest way to resolve this is issue to use the hard disk as a RAM in addition to the main RAM.

The hard disk is a storage medium like RAM. It is slow in access but it is a storage medium just the same. So, how do you do that?

The operating system is programmed to handle such use of hard disk as memory in addition to the RAM.

This usage of hard disk as RAM, as mentioned earlier, is thus called Virtual Memory.

As the name suggests, the idea is to have an imaginary memory or logical memory, that is greater in size than the RAM.

One could say that the Virtual memory overflows from the RAM and on to the hard disk to make up for the required space. This way the programs see the virtual memory as the computer memory to work with.

How the matter is handled between RAM and hard disk is managed internally by the operating system.

Also Read: What is Difference Between Physical Cores and Logical Processors

What is Virtual Memory

There are various ways of implementing the concept of virtual memory, which we will now discuss.

1. Paged Virtual Memory

page virtual memory

Paged Virtual Memory: Source: Wikipedia.com

When we talk about what is virtual memory, the first thing that comes to mind is paged virtual memory.

The virtual memory is a logical memory (software based; not physical) and comprises a virtual address space that exceeds the address space of the RAM.

In case of paged virtual memory, this virtual address space is divided into pages.

Pages are blocks memory of the same size e.g. 4kb in the virtual memory.

So, when the computer is loading an application into memory, it divides it into equal sized blocks exactly the size of a page (4kb) and goes on to fill an array of pages with these application blocks.

These fixed blocks are called frames in physical memory (RAM) and are called pages in logical memory (virtual memory).

Practically speaking, the application pages will be partially stored in the RAM and partially on the hard disk. The pages that are not currently needed go onto the hard disk. The pages are immediately needed get stored on to the physical RAM.

The pages are stored on the hard disk in a swap file – which sequentially stores these pages.

So how does the computer handle the actual storage of applications in the RAM or hard disk?

It does that by mapping the virtual address space with the physical address space. This mapping is done by the operating systems using page tables.

Also Read: What is the Important of Cache Memory

Page Tables

page tables

Page Tables. Source: eyelublog

Page tables are used to hold information about the pages of the VIRTUAL MEMORY.

The information includes flags which tell whether the page is currently stored in the RAM or not.

When the application tries to access a page, and the page table tells that the page is residing in the RAM, the application accesses the RAM and the program continues.

In case the table tells that the page is not residing in RAM, a “page fault exception” is raised which tells the operating system’s “paging supervisor” to arrange for the requested page to be sent to the RAM.

The operating system then swaps a page in RAM with the requested page and returns control to the application.

There are several ways in which the page tables can be implemented.

There could be just one page-table with all the information needed by the system. In this case, the various concurrent applications use different portions of the virtual address space for storage.

Alternatively, there could be a separate table for each application. In this case, every application has its particular page table to provide it with addressing information which corresponds to areas in the physical memory.

There is also the possibility of having a tree of page tables in case of large segments in programs.

Also Read:

Paging Supervisor and Swapping

The paging supervisor component of the operating system manages the translation of addresses of applications using the page tables.

Let’s say that the application tries to access a virtual address that is not available on the RAM.

This will raise a page fault exception and the paging supervisor will look for the page in the hard disk and copy it to the RAM.

Once the page is copied to the RAM, it will then return the control to the application.

Here, if the location in RAM is already occupied, the paging supervisor will have to remove a page from the RAM and swap it with the page that is required.

There are various algorithms that have been designed to manage this page swapping optimally.

Thrashing

At some point ever since you have been using computers, you will have encountered a situation where your computer becomes very slow, jittery or even temporarily gets stuck while the system CONTINUOUSLY keeps accessing the hard disk.

You may experience this even after you have allocated sufficient virtual memory for the system.

This happens due to a phenomenon called thrashing. Thrashing occurs when the operating system is continuously swapping pages to and fro virtual memory and RAM to run a program.

As such, your computer can even crash.

The most probable reason for this situation is that you have too many programs open at once for all of them to have space in the RAM.

When thrashing occurs, it serves as a wake up call that you tofinally need to upgrade your physical RAM and stop relying on the virtual memory.

2. Segmented Virtual Memory

So what is segmented virtual memory? This often comes up when talking about paged virtual memory.

The concept in case of segmented virtual memory again is about having a virtual address space that is much larger than the space available in RAM.

What is different from paging is that the address space is divided into variable sized segments which are allocated dynamically when an application is run.

Application code is segmented and has various modules like stack, program libraries, main program, sub routines etc.

This encourages the use of segments at times for virtual memory instead of pages. The segments contain complete modules of an application.

In order to map the segments in virtual memory with the segments in RAM, segment tables are used.

The addressing is in the form of a segment number and an offset. This type of virtual memory is advantageous because the segments are specifically identified for the application.

When these segments are stored on the hard disk, they can be stored as individual files instead of having a single file storing the complete memory.

There is also the possibility of using the segmenting and paging together. You can have segments consisting of a number of pages.

3. Address Space Swapping

Address Space Swapping

Address Space Swapping

The operating systems also provide the functionality of address space swapping.

This means that entire address space gets where the segments and pages had been stored in RAM, get swapped out or written into swap files for later use.

When the swap-in occurs, the operating system reads back the swap files into the RAM, but in this case, the operating system does not read back those pages that had been paged out at the time of swapping out.

Final Words

So what is virtual memory? Basically, it is a critical way in which an operating system manages the memory to try to give a seamless performance.

The use of virtual memory in computers is extremely advantageous and allows smooth operation of applications, even when the physical memory (RAM) becomes insufficient.

It can be implemented in various ways but in any case, it is a concept that is an integral part of every operating system.

In modern day competition between various brand names for hardware and software, smooth and seamless operation is the key to attract customers and to offer their products at a higher price, given their competitive edge.

Leave a Comment