Virtual Memory in Operating System (original) (raw)

Virtual memory is a memory management technique used by operating systems to give the appearance of a large, continuous block of memory to applications, even if the physical memory (RAM) is limited. It allows larger applications to run on systems with less RAM.

virtual_memory

A virtual memory is what its name indicates- it is an illusion of a memory that is larger than the real memory. We refer to the software component of virtual memory as a virtual memory manager. The basis of virtual memory is the noncontiguous memory allocation model. The virtual memory manager removes some components from memory to make room for other components.

The size of virtual storage is limited by the addressing scheme of the computer system and the amount of secondary memory available not by the actual number of main storage locations.

The History of Virtual Memory

Before virtual memory, computers used RAM and secondary memory for data storage. Early computers used magnetic core in place of main memory and magnetic drums in place of secondary memory. In the 1940s and 1950s, computer memory was very expensive and limited in size. As programs became larger and more complex, developers had to worry that their programs might use up all the available memory and cause the computer to run out of space to work.

In those early days, if program was larger than available space then programmers use the concept of overlaying. If some portion of the code that is not currently used move to overlying if needed it back overwritten to memory this cause extensive programming. This is the reason for developing virtual memory.

In 1956, German physicist **Fritz-Rudolf Güntsch develop virtual memory. The first real example of a virtual memory system was created at the University of Manchester in England while developing the Atlas computer. This system used a method called paging, which allowed virtual addresses (used by programs) to be mapped to the computer’s main memory. The Atlas computer was built in 1959 and started working in 1962.

The first computer with virtual memory was released by Burroughs Corp in 1961. This version of virtual memory do not using paging it oppose paging but support segmentation.

How Virtual Memory Works?

Virtual Memory is a technique that is implemented using both hardware and software. It maps memory addresses used by a program, called virtual addresses, into physical addresses in computer memory.

If these characteristics are present then, it is not necessary that all the pages or segments are present in the main memory during execution. This means that the required pages need to be loaded into memory whenever required. Virtual memory is implemented using Demand Paging or Demand Segmentation.

Types of Virtual Memory

In a computer, virtual memory is managed by the Memory Management Unit (MMU), which is often built into the CPU. The CPU generates virtual addresses that the MMU translates into physical addresses.

There are two main types of virtual memory:

**Paging

Paging divides memory into small fixed-size blocks called pages. When the computer runs out of RAM, pages that aren't currently in use are moved to the hard drive, into an area called a swap file. The swap file acts as an extension of RAM. When a page is needed again, it is swapped back into RAM, a process known as page swapping. This ensures that the operating system (OS) and applications have enough memory to run.

**Demand Paging: The process of loading the page into memory on demand (whenever a page fault occurs) is known as demand paging. The process includes the following steps are as follows:

Demand Paging

Hence whenever a page fault occurs these steps are followed by the operating system and the required page is brought into memory.

**What is Page Fault Service Time?

The time taken to service the page fault is called page fault service time. The page fault service time includes the time taken to perform all the above six steps.

Let Main memory access time is: m
Page fault service time is: s
Page fault rate is : p
Then, Effective memory access time = (p*s) + (1-p)*m

Page vs Frame

A page is a fixed size block of data in virtual memory and a frame is a fixed size block of physical memory in RAM where these pages are loaded. Think of a page as a piece of a puzzle (virtual memory) and a frame as the spot where it fits on the board (physical memory). When a program runs its pages are mapped to available frames so the program can run even if the program size is larger than physical memory.

Segmentation

Segmentation divides virtual memory into segments of different sizes. Segments that aren't currently needed can be moved to the hard drive. The system uses a segment table to keep track of each segment's status, including whether it's in memory, if it's been modified, and its physical address. Segments are mapped into a process's address space only when needed.

read more about - Segmentation

Combining Paging and Segmentation

Sometimes, both paging and segmentation are used together. In this case, memory is divided into pages, and segments are made up of multiple pages. The virtual address includes both a segment number and a page number.

Virtual Memory vs Physical Memory

Feature Virtual Memory Physical Memory (RAM)
**Definition An abstraction that extends the available memory by using disk storage The actual hardware (RAM) that stores data and instructions currently being used by the CPU
**Location On the hard drive or SSD On the computer's motherboard
**Speed Slower (due to disk I/O operations) Faster (accessed directly by the CPU)
**Capacity Larger, limited by disk space Smaller, limited by the amount of RAM installed
**Cost Lower (cost of additional disk storage) Higher (cost of RAM modules)
**Data Access Indirect (via paging and swapping) Direct (CPU can access data directly)
**Volatility Non-volatile (data persists on disk) Volatile (data is lost when power is off)

**What is Swapping?

Swapping is a process out means removing all of its pages from memory, or marking them so that they will be removed by the normal page replacement process. Suspending a process ensures that it is not runnable while it is swapped out. At some later time, the system swaps back the process from the secondary storage to the main memory. When a process is busy swapping pages in and out then this situation is called thrashing.

Swapping

Swappinghierar

**What is Thrashing?

At any given time, only a few pages of any process are in the main memory, and therefore more processes can be maintained in memory. Furthermore, time is saved because unused pages are not swapped in and out of memory. However, the OS must be clever about how it manages this scheme. In the steady state practically, all of the main memory will be occupied with process pages, so that the processor and OS have direct access to as many processes as possible. Thus when the OS brings one page in, it must throw another out. If it throws out a page just before it is used, then it will just have to get that page again almost immediately. Too much of this leads to a condition called Thrashing. The system spends most of its time swapping pages rather than executing instructions. So a good page replacement algorithm is required.

Degree-of-multiprogramming

In the given diagram, the initial degree of multiprogramming up to some extent of point(lambda), the CPU utilization is very high and the system resources are utilized 100%. But if we further increase the degree of multiprogramming the CPU utilization will drastically fall down and the system will spend more time only on the page replacement and the time taken to complete the execution of the process will increase. This situation in the system is called thrashing.

**Causes of Thrashing

Thrashing occurs in a computer system when the CPU spends more time swapping pages in and out of memory than executing actual processes. This happens when there is insufficient physical memory, causing frequent page faults and excessive paging activity. Thrashing reduces system performance and makes processes run very slowly. There are many cause of thrashing as discussed below.

**1. High Degree of Multiprogramming

If the number of processes keeps on increasing in the memory then the number of frames allocated to each process will be decreased. So, fewer frames will be available for each process. Due to this, a page fault will occur more frequently and more CPU time will be wasted in just swapping in and out of pages and the utilization will keep on decreasing.

For example:
Let free frames = 400
**Case 1: Number of processes = 100
Then, each process will get 4 frames.

**Case 2: Number of processes = 400
Each process will get 1 frame.
Case 2 is a condition of thrashing, as the number of processes is increased, frames per process are decreased. Hence CPU time will be consumed just by swapping pages.

**2. Lacks of Frames

If a process has fewer frames then fewer pages of that process will be able to reside in memory and hence more frequent swapping in and out will be required. This may lead to thrashing. Hence a sufficient amount of frames must be allocated to each process in order to prevent thrashing.

**Recovery of Thrashing

**Performance in Virtual Memory

*Effective access time (EAT) = (1-p) Memory Access Time + p * Page fault time.
Page fault time = page fault overhead + swap out + swap in +restart overhead

The performance of a virtual memory management system depends on the total number of page faults, which depend on "paging policies" and "frame allocation"

read more about - Techniques to handle Thrashing

**Frame Allocation

A number of frames allocated to each process in either static or dynamic.

**Paging Policies

What are the Applications of Virtual memory?

Virtual memory has the following important characteristics that increase the capabilities of the computer system. The following are five significant characteristics of Lean.

How to Manage Virtual Memory?

Here are 5 key points on how to manage virtual memory:

1. Adjust the Page File Size

2. Place the Page File on a Fast Drive

3. Monitor and Optimize Usage

4. Disable Virtual Memory for SSD

5. Optimize System Settings

**What are the Benefits of Using Virtual Memory?

**What are the Limitation of Virtual Memory?

Conclusion

In conclusion, virtual memory is a crucial feature in operating systems that allows computers to run larger applications and handle more processes than the physical RAM alone can support. By using techniques like paging and segmentation, the system extends the available memory onto the hard drive, ensuring that the operating system and applications can operate smoothly. Although virtual memory can introduce some performance overhead due to the slower speed of hard drives compared to RAM, it provides significant benefits in terms of memory management, efficiency, and multitasking capabilities.

read more about - Virtual Memory Questions