What Is Virtual Memory and How Does It Work?

 

In this technology-dominated age, virtual memory can be an important asset for both business owners and employees to store data and run applications. Virtual memory can help you optimize your computer's system and attain a high level of efficiency and security so you can keep your computer running at its best and protect your work. 

In this article, we define virtual memory and explain how it works, explain the two ways computers handle virtual memory—paging and segmenting—and provide examples to help you understand its uses.

What is virtual memory?

Virtual memory is a technique used in computing to optimize memory management by transferring data between different storage systems, such as random access memory (RAM) and disk storage. A virtual memory system has many advantages, including:

  • Freeing applications from having to compete for shared memory space and allowing multiple applications to run at the same time

  • Allowing processes to share memory between libraries (a collection of code that provides the foundation for a program's operations)

  • Improving security by isolating and segmenting where the computer stores information

  • Increasing the amount of memory available by working outside the limits of a computer's physical memory space

  • Optimizing central processing unit (CPU) usage

Virtual memory is a built-in component of most modern desktop computers. It's incorporated into a computer's CPU and is a more cost-effective method for managing memory than expanding the computer's physical memory storage system.

However, some specialized computers might not rely on virtual memory because it could cause inconsistencies in the computer's processing. Professionals in certain industries, such as in scientific or statistical modeling, may avoid using virtual memory for specific tasks that require stability and predictability. Most everyday personal or business computers don't need this level of consistency and benefit from the advantages of virtual memory far more than from the predictability of other types of memory systems.

How does virtual memory work?

Virtual memory uses both the computer's software and hardware to work. It transfers processes between the computer's RAM and hard disk by copying any files from the computer's RAM that aren't currently in use and moving them to the hard disk. By moving unused files to the hard disk, a computer frees up space in its RAM to perform current tasks, such as opening a new application. If the computer later needs to use its RAM for a more urgent task, it can again swap files to make the most of the available RAM.

RAM is a limited resource stored on chips that are built into the computer's CPU. Installing more RAM chips can be expensive, so virtual memory allows the computer to move files between systems as needed to optimize its use of the available RAM.

Example: A business owner might use their computer's virtual memory system when running multiple applications at once. For example, the user might try to load their email in their browser window while also running a word processing software, a shift scheduling software and a content management system at the same time. Since the computer needs to run several programs at once, it might adjust its memory usage to optimize its ability to open the user's email application while maintaining the operations of the other software programs.

To open the user's email, the computer's operating system (OS) may have to initiate its memory management unit (MMU) to search for the page or segment table containing the virtual or physical address for the process that can open the email application. Once located, the OS can either move the application to the computer's RAM to open the application, or it can access the application if it's already stored in RAM. If the RAM is near its limit, the computer may move another file from the RAM to another storage space to reduce its RAM usage.

Types of virtual memory

The two ways computers handle virtual memory are through paging and segmenting. Here are the differences between these types of virtual memory:

Paging

This type of virtual memory works by separating memory into sections called paging files. When a computer reaches its RAM limits, it transfers any currently unused pages into the part of its hard drive used for virtual memory. The computer performs this process using a swap file, which is a designated space within its hard drive for extending the virtual memory of the computer's RAM. By moving unused files into its hard drive, the computer frees its RAM space for other memory tasks and ensures that it never runs out of real memory.

As part of this process, the computer uses page tables, which translate virtual addresses into the physical addresses that the computer's MMU uses to process instructions. The MMU communicates between the computer's OS and its page tables. When the user performs a task, the OS searches its RAM for the processes to conduct the task. If it can't find the processes to perform the task in RAM, the MMU prompts the OS to move the required pages into RAM and uses a page table to note the new storage location of the pages.

Segmenting

Segmentation is another method of managing virtual memory. A segmentation system divides virtual memory into segments of varying lengths and moves any segments not in use from the computer's virtual memory space to its hard drive. Similar to page tables, segment tables track whether the computer is storing the segment in memory or a physical address.

Segmentation differs from paging because it divides memory into sections of varying lengths, while paging divides memory into sections of equal size. With paging, the hardware determines the size of a section, but the user can determine the length of a segment in a segmentation system. Although segmentation is often slower than paging, it offers the user more control over how to divide memory and may make it easier to share data between processes. However, many casual computer users may prefer a paging system because it automatically handles memory divisions.

Limitations of virtual memory

Although virtual memory has many advantages, here are some of its limitations:

  • Virtual memory is often slower than physical memory, so most computers prioritize using physical memory when possible.

  • It requires additional hardware support to move data between a computer's virtual and physical memory.

  • The amount of storage virtual memory can provide depends on the amount of secondary storage a computer has.

  • If the computer only has a small amount of RAM, virtual memory can cause "thrashing," which is when the computer must constantly swap data between virtual and physical memory, resulting in significant performance delays.

  • It can take longer for applications to load or for a computer to switch between applications when using virtual memory.

Virtual memory vs. physical memory

The two most significant differences between virtual memory and physical memory are speed and cost. Computers that rely on physical memory tend to be faster than computers relying on virtual memory. However, increasing a computer's physical memory capacity is more expensive than implementing a virtual memory system. For these reasons, most computers use their physical memory system—their RAM—to maintain the speed of their processing before using their virtual memory system. The computer only uses its virtual memory when it runs out of RAM for storage.

However, users also have the option to expand their RAM. Installing more RAM can resolve computer delays caused by frequent memory swaps. The amount of RAM a computer has depends on how much the user or manufacturer installs. Comparatively, the size of the computer's hard drive determines its virtual memory capacity. When choosing a computer, you may prefer one with more RAM if you're someone who runs many applications at a time. If you work with only one or two computer applications most of the time, you may not need as much RAM.

The advantages of using this system are as follows

More processes can be maintained in the main memory:

One or more numerous processes will be in the ready state at any particular time, leading to more efficient utilisation of the processor.

Processes may be larger than all of the main memory:

This advantage solves one of the most fundamental restrictions in programming. A process, which requires more than the main memory capacity can be executed due to demand paging. The operating system itself loads pages of a process in main memory as required.

It allows greater multiprogramming levels by using less of the available primary memory for each process.

Swapping:

Swapping is the process that removes all of its pages from memory, or allowing them to be removed by the normal page replacement process.

Suspending a process ensures that it is not executable while it is swapped out. When required, the system swaps back the process from the secondary storage to main memory.

Swapping pages in and out is referred to as “Thrashing”.

Thrashing occurs when the computer’s virtual memory resources are overused, which will lead to a constant state of paging and page faults, thus inhibiting most application level processing. This will cause the computer performance to degrade or collapse. Thrashing can possibly continue indefinitely until either the user closes the currently running application(s) or any active processes to free up additional virtual memory resources.

(so next time your systems stops, or lags, think about thrashing!)

Disadvantages of Demand Paging

  • Individual programs face extra latency (more time to load) when they access a page for the first time.
  • Low powered, low cost embedded systems could possibly not have a memory management unit that support page replacement.
  • Page replacement algorithms are complex.
  • Security risks, including timing attacks are at risk.
  • Thrashing as mentioned above.

Types of Page Replacement Methods

There are three main types of Page replacements methods:

FIFO (First in First Out):

The oldest page in the main memory is the one which will be selected for replacement.

Relatively simple to implement, keep a list and replacing pages from the tail and add new pages at the head.

(tail = end of the line queue) (head = top of the line queue) 

Optional Algorithm:

This algorithm has the lowest page fault rate of all the algorithms.

This algorithm works as follows: when a page requires to be swapped in, the operating system will swap out the page whose next use will occur farthest in the future. For instance, a page that will not be used for 15 seconds will be swapped for a page that will be used in the next 0.9 seconds.

LRU Page Replacement:

Least recently used page replacement is an algorithm which works on the theory that pages, which had been most heavily used in the past few instructions are most likely to be used heavily in the next few instructions too. While LRU could potentially provide near optimal performance, they are expensive to implement in practice, moreover there are few implementation methods for this algorithm that try to reduce the cost but yet have the same performance.

Demand Segmentation:

Memory segmentation is a technique, which refers to dividing the computer primary memory into segments or sections.

Demand segmentation allows for pages that are often referenced with each other to be brought into memory together, this action will decrease the number of page faults.

Advantages of Virtual Memory

  • Allows more than one program to be executed at the same time.
  • Common data or code may be shared between memory.
  • The processes may become even larger than the overall physical memory, which means virtual memory can be used to offload some of the memory, which isn’t being used in the physical memory.
  • Virtual memory increases the overall memory on a system without adding RAM, this is advantageous as virtual memory is less expensive.
  • Eliminates external fragmentation.
    • External fragmentation occurs when free memory is divided into smaller blocks, and is interspersed by allocated memory. External fragmentation is regarded to be a disadvantage in storage allocation algorithms, when they fail to order memory used by programs efficiently.
  • Less input/output is required, which leads to faster and easy swapping of processes.

Memory holes: Memory holes could cause fragmentation, though virtual memory solves this issue as virtual memory doesn’t access the physical memory management unit, furthermore, every program has its own mapping and using the mapping we can put our program data wherever we want in the RAM.

Disadvantages of Virtual Memory

  • If the system relies to much on virtual memory, it may cause a decrease in performance.
  • The more virtual memory that’s being used, the less disk space a user has for storage.
  • Time it takes to read from a magnetic disk is greater than the time to access RAM, therefore swapping should be avoided wherever performance is important. Ultimately meaning that it would take greater time to switch between applications.

Virtual Memory vs. Physical Memory

Physical Memory:

In a computer system, physical memory refers to the primary memory. It can also be regarded as volatile memory, as for it to retain data, it requires a continuous flow of power, that being said a big disadvantage of physical memory is that if power failure or any kind of interruptions occur to the system, they can cause the data to be erased from the physical memory.

Unlike virtual memory, the CPU can directly access physical memory as it holds the programs in the execution lineup.

Once the execution of a program is completed, the program and its data will go to the hard disk and that physical memory slot will be allocated to a new program. 

Difference between Virtual and Physical Memory:

The main difference between both types of memories is that physical memory refers to the actual Random Access Memory (RAM) on the system, which is attached to the motherboard, however virtual memory is a memory management technique, which allows users to execute programs larger than the actual physical memory capacity.

Memory type:

  • Physical memory is an actual memory
  • Virtual memory is logical memory

Speed:

  • Physical memory is faster than virtual memory

Technique:

  • Physical memory utilises swapping technique, whereas virtual memory uses paging.

Size:

  • Physical memory is limited to the size of RAM on the system, on the other hand, virtual memory is limited to the size of the hard disk.

CPU:

  • Physical memory can directly access the RAM chip unlike virtual memory.

Summary and Facts

What is Virtual Memory?

Virtual memory describes a section of volatile memory created temporarily on the storage drive. Virtual memory is used when a computer is running many processes at one and the RAM becomes overloaded and running low.

Part of the storage drive on the operating system is available to use in conjunction with Random Access Memory (RAM). However virtual memory is much slower than RAM as the processing power is being utilised by moving data around, instead of executing instructions.

Virtual Memory Characteristics

The virtual memory mechanism works by making use of both hardware and software. Virtual memory carries out its job by mapping memory address used by a program, which are referred to as virtual addresses, into a physical address in the computer memory.

Virtual memory is implemented using both Demand Paging and Demand Segmentation.

Types of Page Replacement Methods

There are three main types of Page replacements methods:

  • FIFO (First in First Out)
  • Optional Algorithm
  • LRU Page Replacement

Advantages of Virtual Memory:

  • Allows more than one program to be executed at the same time.
  • Common data or code may be shared between memory.
  • Offloading tasks from main memory.
  • Virtual memory increases the overall memory on a system without adding RAM.
  • Eliminates external fragmentation.
  • Less input/output is required

Disadvantages of Virtual Memory:

  • If the system relies to much on virtual memory, it make cause a decrease in performance.
  • The more virtual memory that’s being used, the less disk space a user has for storage.
  • Time it takes to read from a magnetic disk is greater than the time to access RAM, therefore swapping should be avoided wherever performance is important. Ultimately meaning that it would take greater time to switch between applications.

Post a Comment

0 Comments