What is Virtual Memory and How it Works?

260
What-is-Virtual-Memory

Virtual memory is a way for an application to function as if the system had more RAM than it actually does. Virtual memory works by swapping non required data to a file on the hard disk called a swap file or page file. This data is then called up and used only when it is required.

More About Virtual Memory

The virtual memory is managed by a Virtual Memory Manager present within an Operating System. It maps virtual addresses that belongs to the address space of a running process to physical memory page of the computer. This ensures that each process has enough virtual memory to run efficiently and does not invade the memory of other processes. Virtual memory addresses RAM for zero and when RAM is filled, memory is written to it.

Virtual memory dramatically increases the working capacity of your computer because it is often not necessary to work in the RAM of your computer or in your workspace. If a user has too many open programs, excessive page switching occurs, slowing down the application. Virtual memory simulates what actually exists and allow your computer to run large programs or multiple programs simultaneously.

What questions will be answered:

  • How virtual memory and physical memory are different?
  • Why virtual Memory is needed?
  • How does virtual memory work?
  • What are the types of virtual memory?
  • Advantages and Disadvantages of virtual memory

Virtual memory is beneficial, let’s know in what ways!

How Virtual Memory and Physical Memory Are Different?

Physical memory is the only memory which is directly accessible to the CPU. The instructions stored on the physical memory are read by the CPU  and it than executes them. The data after execution is also stored on the Physical memory.

Virtual memory as the name suggests is virtual, i.e. not real. The Operating System (OS) uses virtual memory for memory management where non-contiguous memory is presented as contiguous memory to the software. The cases where RAM falls short of memory, OS allocates a portion of the hard drive that acts as RAM, which is termed as virtual memory.

Memory Allocation

Information is allocated by physical memory through a process called “first in, last out”. In this case the information is placed on the stack. Whereas virtual memory makes use of paging process, in which pages in fixed sizes are laid across the hard drive.

Memory Size

Physical memory is limited bythe size of RAM installed on a computer. Whereas virtual memory is limited by the hard drive size thus has more storage space.

Considerations

To increase the performace of a computer system that uses virtual memory often, more RAM is to be installed. The virtual memory settings are controlled by the operating system.Also the read/write speed of a hard drive is a lot slower than that of RAM

Why Virtual Memory is Needed?

Virtual memory came into existence seeing the high cost linked to physical RAM and even today RAM is more expensive than storage devices like solid state drives and hard disks. In order to reduce the cost, virtual memory is used in combination with physical RAM. As to equip a system with more RAM is on the higher side of price.

All computer systems have a limit on the RAM that can be installed, but virtual memory allows the system to operate beyond those limits.  Making use of virtual memory is brings no additional cost as it uses the existing space. It is the easiest way to let a system use memory more than the physical memory available on the system.

How Does Virtual Memory Work?

  • Virtual memory is a part of computer’s operating system. It has gained the popularity as it helps cut down the cost that RAM expansion could bring. When an application runs, it stores the program threads location and data at a virtual address, whereas RAM is the physical address where the data is actually stored. 
  • Most computer system come with 32 or 64 megabytes of RAM, which is used by only the CPU. Sadly, this RAM is not sufficient to run many programs at a time.

To understand it better – If you try to run multiple programs on a 32 megabytes RAM, if your system is unable to handle that it will throw a message saying “Sorry, you cannot load any more applications at this time. Please close running applications to load new”.

If you have a system with RAM and virtual memory, in this case is you try to run number of applications more than what RAM can handle at same time, virtual memory keeps an eye on RAM and applications that are not being used recently, copies them onto the hard disk. This actions frees up the space on RAM and lets you run the new application without closing any.

The process of copying data to hard disk is an automatic process that runs in background, without letting user know what is happening when he/she is trying to run more number of applications at a time. This will also make you feel that your32 megabytes RAM computer has unlimited space.­

­In case where operating system has to frequently swap information to and fro between RAM and the hard disk. This process is known as thrashing. During this process virtual memory is used frequenntly which makes your computer extremely slow.

What Are the Types of Virtual Memory?

All the memory operations inclusive of managing a virtual memory is handled by a memory management unit also known as MMU. Mostly MMU is integrated within the CPU and handles the in different ways. Based on how the virtual memory is handled, you can also say that Virtual memory is generally of 2 types: Paged and Segmented.

Paged Memory 

It is an approximately 4kb of memory , divided into sections or paging files. When a computer RAM is in use at its fullest, the computer transfers the unused pages to the virtual memory using a  swap file.

A swap file is a space set aside on the hard drive as the virtual memory extensions of the computer’s RAM. Page swapping is a process which sends the swap file to the RAM which comes into play when the swap file is required. This system ensures that computer’s OS and applications don’t run out of real memory.

Segmentation

Under this approach the the virtual memory is divided into segments of different lengths. There are segment tables to track the segmented processes or information. The segment table shows whether or not a segment is present in the memory, its modification time and also its physical address.

In some cases, virtual memory combines both paging and segmentation.  When this is the case, the memory gets divided into frames or pages. The segment then takes up multiple pages and the page and segment numbers are both included in the virtual address.

Advantages and Disadvantages of Virtual Memory

From the time when Virtual Memory was first developed, the cost of RAM has reduced, it is relatively inexpensive now. Even after that virtual memory is employed on many computer systems. And below listed are the pros and cons of virtual memory that you must know:

What are the Benefits of Using Virtual Memory?

  • Inexpensive way of increasing memory, which is comparatively less in price than RAM.
  • Allows multiple applications to run at the same time.

  • Allows bigger applications to run on systems that do not have enough physical RAM to run them.
  • Increases memory in a system which already have maximum RAM, which is supported by the system hardware and operating system.

What are the disadvantages of Virtual Memory?

  • Though Virtual memory increases the memory of a computer system, but its performance is not same as of RAM.
  • Can slowdown the system’s overall performance or impact it negatively.
  • Virtual memory consumes the storage space.

Wrapping Up

Virtual memory is an inexpensive alternative to buying RAM. It has both the pros and cons that you must know before going for it. Virtual memory is pocket-friendly but performance slowdown is one of the disadvantages of virtual memory that you might not like. From what virtual memory is to how it works has been discussed. If you find this article helpful, tell us in the comments below!

Leave a Reply !!

This site uses Akismet to reduce spam. Learn how your comment data is processed.