Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Virtual Memory in Operating System (OS) | Core Computer Science

Ujjwal Abhishek
Ujjwal Abhishek

What is Virtual Memory?

Virtual Memory is a storage technique that creates an illusion to the user of a very large main memory. It creates an illusion that the secondary memory is also a part of the main memory. The extra memory other than the main memory which is used for storing the pages of processes is referred to as the Virtual Memory. 

It helps in loading bigger-size processes than the size of the main memory. It also helps in increasing the degree of multiprogramming by storing more and more processes inside the main memory.

The whole process is not required to be loaded in the main memory for the execution. Only a part of a process is required at a time. It means a page of a process is loaded in the main memory if required else not. Virtual Memory is implemented using Demand Paging. It can also be implemented using Demand Segmentation. 

Demand Paging

CPU generates a logical address which is converted to a physical address by MMU (Memory Management Unit). When a page referenced by CPU is not found in the main memory, this is called Page Fault. Demand Paging is a process of copying a page from the disk into the main memory whenever required or when a Page Fault occurs.

This is done by the Operating System through the following steps:

  • When a CPU references to a Page that is not present in the main memory, a Page Fault occurs.
  • To execute the process, the Operating System needs to bring the reference page in the main memory.
  • The Operating System then searches for the required page in the Disk Storage. It is a very slow process.
  • Then it stores the page in one of the vacant frames of the main memory.
  • Then Page table is updated according to that frame number for future references.
  • Then the CPU executes the program again.

Swapping

Swapping is a process done by the Operating System for swapping in and out of the process in the main memory. When the pages of a process are not required then it is swapped out from the main memory and when they are required they are swapped in the memory again.

Thrashing

Thrashing is a condition in which the CPU utilization decreases drastically. It happens when the CPU is too busy Swapping in and Swapping out the pages from the main memory. One of the major reasons for this is the high degree of multiprogramming.

When there are too many processes in the main memory, the number of frames allocated to a process goes down, and the swapping of pages in and out becomes more frequent. This leads to more number of page faults and the CPU gets busy servicing those page faults which ultimately leads to thrashing.

Advantages of Virtual Memory

  1. It helps in executing bigger processes with less RAM.
  2. Large Applications can be run with less RAM.
  3. It increases the degree of multiprogramming.

Disadvantages of Virtual Memory

  1. It is more time-consuming as accessing disk storage is very slow.
  2. The Applications may run slower.
  3. Part of hard disk storage is also used in this process.
Ujjwal Abhishek
Ujjwal Abhishek
Ujjwal is final-year CSE Undergraduate, a competitive coder, and a web developer passionate about problem-solving and data structures and algorithms.
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet