Virtual Memory

KS3 Computer Science

11-14 Years Old

48 modules covering EVERY Computer Science topic needed for KS3 level.

GCSE Computer Science

14-16 Years Old

45 modules covering EVERY Computer Science topic needed for GCSE level.

A-Level Computer Science

16-18 Years Old

66 modules covering EVERY Computer Science topic needed for A-Level.

GCSE Memory and Data Storage (14-16 years)

  • An editable PowerPoint lesson presentation
  • Editable revision handouts
  • A glossary which covers the key terminologies of the module
  • Topic mindmaps for visualising the key concepts
  • Printable flashcards to help students engage active recall and confidence-based repetition
  • A quiz with accompanying answer key to test knowledge and understanding of the module

A-Level Data Storage Resources (16-18 years)

  • An editable PowerPoint lesson presentation
  • Editable revision handouts
  • A glossary which covers the key terminologies of the module
  • Topic mindmaps for visualising the key concepts
  • Printable flashcards to help students engage active recall and confidence-based repetition
  • A quiz with accompanying answer key to test knowledge and understanding of the module

One of the main processes in an operating system is memory management. This technique allows the processes (which allow applications/programs/tasks that you require to function) to move back and forth between the main memory and hard disk memory during the time of execution.

Memory management keeps track of all memory locations in order to allocate processes and to free the memory in order for other processes to use.

There are two types of memory which modern operating systems use, physical memory, and virtual memory.

Physical memory is the primary memory that current applications and programs which are in use make use off. Virtual memory extends the capacity of the main memory to execute large programs using the hard disk (This article will focus on virtual memory).

What is Virtual Memory?

Between the 1940’s and 1950’s, many of the larger programs had to contain logic for managing primary and secondary memory, such as overlaying (overlaying refers to transferring a block of program code or other data into main memory replacing what was already stored).

Therefore virtual memory was introduced not only to extend the memory that was already there (primary memory), but it also allowed multiprogramming and multitasking, with had increased the productivity of programmers.

Early systems had divided (partitioned) memory between multiple programs without the use of virtual memory.

The first true virtual memory system was implemented in the University of Manchester, which was used to create a one level storage system using a paging mechanism. 

In most operating systems (i.e. like computers) nowadays, virtual memory is a common part of the system, this is due to providing great benefits to users, at a relatively low cost.

Virtual memory is a peripheral in the operating system, which enables a computer to be able to compensate shortages of physical memory (RAM) by transferring pages of data from Random Access Memory (RAM) to disk storage to deny overloading, furthermore virtual memory is designed to work temporary in combination with RAM.

Therefore, when RAM runs close to full capacity, virtual memory can move data from the RAM to a space which is referred to as a “paging file”. This allows the RAM on the system to free up space so that the computer can continue with the main execution/task.

Lots of the computer systems that are in use, have a 32 or 64 megabytes of RAM available for the CPU, though despite having that, it will not be sufficient enough to run all of the required programs that users expect to run at once on RAM solely.

Virtual memory, also regarded to be logical memory, is a memory management technique performed by the operating system. The main use of virtual memory is, it allows the users to use more memory for applications/programs/tasks than the available physical memory (RAM) on the system.

One of the main advantages of virtual memory is it ensures memory protection by converting the memory address to the corresponding physical address. It does this by extending the use of physical memory by using the hard disk, though it prevents accessing the main memory directly.

The size of virtual memory (storage) is limited by the addressing schemes of the computer system and the amount of secondary memory available.

Addressing scheme: This is a requirement for communications in a computer network.

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 is referred to as virtual addresses, into a physical address in the computer memory.

With that in mind, all memory references are initially logical addresses that will be dynamically translated into a physical address at run time. The benefit of this is that the process can be swapped in and out of main memory, this allows it to occupy different places in the main memory at different times during the course of execution.

The process, which is getting executed, may be broken into a number of different pieces and these pieces cannot be continuously located in the main memory during execution. This is allowed due to the combination of dynamic run time address translation and the use of page or segment tables.

Dynamic address translation: This is a process of translating a virtual address during a storage reference into the corresponding real address.

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

Demand Paging:

Demand paging is a type of swapping that is done in virtual memory systems. Demand paging as it says from the title, only copies data from the disk to the RAM if the data is required by some program, therefore meaning that the data will not be when the data is already available on the memory. This can be referred to as “lazy evaluation”, because only the demanded pages of memory are being swapped from the secondary storage (disk space) to the main memory.

Another type of paging which virtual memory uses is Anticipatory Paging, this is when the operating system attempts to anticipate data that will be needed next and copies it to the RAM before it is actually required.

The process of Demand paging occurs like so:

Page table: A page table is the data structure used by a virtual memory system in a computer operating system to store the mapping between visual address and physical addresses.

  1. The CPU generates an interrupt indicating memory access fault if the CPU tries to refer a page that is currently not available in the main memory.
  2. The operating systems places the interrupted process in a blocking state. Though for the execution to proceed, the operating system must bring the required page into the memory.
  3. The operating system will search for the required page in the logical address space.
  4. The page that is required will be brought to the physical address from the logical address. Page replacement algorithms are used for the decision making of replacing the page in physical address space.
  5. The page table will be updated accordingly.
  6. A signal will then be sent to the CPU to continue the election of the program and it will place the process back into the ready state.

Therefore, whenever a page fault occurs, the steps above are followed by the operating system and the required page is brought back into memory.

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.

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.

Virtual MemoryPhysical Memory
A memory management technique that creates an illusion to users of a larger physical memoryPhysical memory is actual RAM, and a form of computer data storage that stores currently executing programs
Uses pagingUses a the swapping technique
In comparison between the two types of memory, virtual memory is slower than physical memoryPhysical memory is faster than virtual memory
Limited by the size of the hard diskLimited to the size of the RAM chip
Cannot directly access the CPUPhysical memory can access the CPU directly

References:

  1. https://pediaa.com/what-is-the-difference-between-physical-and-virtual-memory/
  2. https://www.cbronline.com/what-is/what-is-virtual-memory-4929986/
  3. https://www.bbc.co.uk/bitesize/guides/zmb9mp3/revision/8
  4. https://en.wikipedia.org/wiki/Fragmentation_(computing)#:~:text=External%20fragmentation%20arises%20when%20free,memory%20used%20by%20programs%20efficiently.
  5. https://practice.geeksforgeeks.org/problems/what-are-benefits-of-virtual-memory
  6. https://www.guru99.com/virtual-memory-in-operating-system.html
  7. https://en.wikipedia.org/wiki/Demand_paging#:~:text=In%20computer%20operating%20systems%2C%20demand,method%20of%20virtual%20memory%20management.&text=It%20follows%20that%20a%20process,are%20located%20in%20physical%20memory.
  8. https://askanacademic.com/computing-and-ict/virtual-memory-and-paging-disadvantages-thrashing-1200/
  9. https://www.interserver.net/tips/kb/virtual-memory-demand-paging/
  10. https://en.wikipedia.org/wiki/Thrashing_(computer_science)#:~:text=In%20computer%20science%2C%20thrashing%20occurs,computer%20to%20degrade%20or%20collapse.
  11. https://www.tutorialspoint.com/operating_system/os_virtual_memory.htm
  12. https://en.wikipedia.org/wiki/Memory_segmentation
  13. https://www.geeksforgeeks.org/virtual-memory-in-operating-system/