How Does Virtual Memory Work?
Tags: os, beginners, tutorial, computerscience
Published: 2019-10-20 03:47:54

<!-- wp:paragraph -->
<p>Have you ever wondered😲, How game of size 8 GB is running on a computer has 4 GB of RAM only? or You can play multiple movies simultaneously combined size more than RAM size? If you are a software developer, you may come across a word like multi-tasking or multiprocessing which is a key concept behind this. In other words, it creates virtual memory which is a memory management technique. Here we will see how it works</p>
<!-- /wp:paragraph -->

> /!\\\: Originally published @ [www.vishalchovatiya.com](http://www.vishalchovatiya.com).

<!-- wp:heading -->
<h2><span style="color: #000000;">How Does Virtual Memory Work</span>?</h2>
<!-- /wp:heading -->

<!-- wp:image {"id":1895,"align":"left"} -->
<div class="wp-block-image"><figure class="alignleft"><img src="http://www.vishalchovatiya.com/wp-content/uploads/How-Does-Virtual-Memory-Work.jpg" alt="" class="wp-image-1895"/></figure></div>
<!-- /wp:image -->

<!-- wp:list -->
<ul><li><span style="color: #000000;">Let’s say that an OS&nbsp;needs 120 MB of memory in order to hold all the running programs</span>.</li><li><span style="color: #000000;">But there’s currently only 50 MB of available physical memory stored on the RAM chips.</span></li><li><span style="color: #000000;">The OS will then set up 120 MB of virtual memory &amp; will use a program called the virtual memory manager(VMM) to manage that 120 MB.</span></li><li><span style="color: #000000;">The VMM will create a file on the hard disk that is 70 MB (120 – 50) in size to account for the extra memory that’s needed.</span></li><li><span style="color: #000000;">The OS&nbsp;will now proceed to address memory as if there were actually 120 MB of real memory stored in the RAM, even though there’s really only 50 MB.</span></li><li><span style="color: #000000;">It is the responsibility of the VMM to deal with the fact that there is only 50 MB of real memory.</span></li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>/!\:</strong> If you want to know in-depth of virtual memory implementation, you are at the wrong place. This article will only clear your concept of "How does virtual memory work"!</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2><span style="color: #000000;">Legends</span></h2>
<!-- /wp:heading -->

<!-- wp:table {"className":"is-style-stripes"} -->
<table class="wp-block-table is-style-stripes"><tbody><tr><td>Physical Memory<br>Or<br>Main Memory </td><td> RAM </td></tr><tr><td> Secondary Memory</td><td> HDD or SSD </td></tr><tr><td> Virtual Memory </td><td>Illusion created by OS for optimization(You can say this definition too) </td></tr><tr><td> VMM </td><td> Virtual Memory Manager </td></tr><tr><td> Process Address Space </td><td> Set of logical addresses that a process references in its code(provided by VMM) </td></tr><tr><td> OS </td><td> Operating system(I think this is not needed) </td></tr><tr><td> AKA </td><td> Also known as  </td></tr></tbody></table>
<!-- /wp:table -->

<!-- wp:heading -->
<h2><span style="color: #000000;">How Does VMM Work?</span></h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><span style="color: #000000;">VMM creates a file on the hard disk that holds the extra memory which&nbsp;needed by the OS, which in our case is 70 MB in size.</span></li><li><span style="color: #000000;">This file called a paging file (AKA swap-file/page-frames) &amp; plays an important role in virtual memory(</span>i.e. How does virtual memory work)<span style="color: #000000;">.</span></li><li><span style="color: #000000;">The paging file combined with the RAM accounts for all of the memory.</span></li><li><span style="color: #000000;">Whenever the OS needs a ‘block’ of memory that’s not in the main(RAM) memory, the VMM takes a block from the real memory that hasn’t used recently, writes it to the paging file &amp; then reads the block of memory that the OS&nbsp;needs from the paging file.</span></li><li><span style="color: #000000;">The VMM then takes the block of memory from the paging file(which needed currently) &amp; moves it into the main&nbsp;memory – in place of the old block.</span></li><li><span style="color: #000000;">This process is called swapping (also known as paging) &amp; the blocks of memory that are swapped, called pages. There are several algorithms for this process, called <a rel="noreferrer noopener" aria-label="Page Replacement Algorithms (opens in a new tab)" href="https://en.wikipedia.org/wiki/Page_replacement_algorithm" target="_blank"><span style="color:#1e0bf1" class="color">Page Replacement Algorithms</span></a>.</span></li><li><span style="color: #000000;">The group of pages that currently exist in RAM &amp; that is dedicated to a specific process, is known as the working set for that process.</span></li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2><span style="color: #000000;">Why Virtual Memory Used?</span></h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><span style="color: #000000;">There are two reasons why one would want this</span>: <ol><li><span style="color: #000000;">To allow the use of programs that are too big to physically fit in memory.</span></li><li><span style="color: #000000;"> The other reason is to allow for multitasking.</span></li></ol></li><li><span style="color: #000000;">Before virtual memory existed, a word processor, e-mail program, &amp; browser couldn’t be run at the same time unless there was enough memory to hold all three programs at once.</span></li><li><span style="color: #000000;">This would mean that one would have to close one program in order to run the other, but now with virtual memory, multitasking is possible even when there is not enough memory to hold all <a rel="noreferrer noopener" aria-label="executing programs (opens in a new tab)" href="http://www.vishalchovatiya.com/program-gets-run-linux/" target="_blank"><span style="color:#3f1bf2" class="color">executing programs</span></a> at once</span>.</li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2><span style="color: #000000;">Which OS Implemented Virtual Memory?</span></h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><span style="color: #000000;">Some of the most popular OSs like Windows, Mac OSX, &amp; Linux implemented VMM.</span></li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2><span style="color: #000000;">Virtual Memory Can Slow Down Performance(Thrashing)!</span></h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><span style="color: #000000;">If the size of virtual memory is quite large in comparison to the main&nbsp;memory, then more swapping to &amp; from the hard disk will occur as a result.</span></li><li><span style="color: #000000;">Accessing the hard disk is far slower than using the main&nbsp;memory(RAM).</span></li><li><span style="color: #000000;">Using too many programs at once in a system with an insufficient amount of RAM results in constant disk swapping – also called thrashing, which can really slow down a system’s performance.</span></li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2><span style="color: #000000;">Terms Associated With Virtual Memory</span></h2>
<!-- /wp:heading -->

<!-- wp:heading {"level":4} -->
<h4><strong>Page </strong></h4>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><span style="color: #000000;">In the Paging Scheme, the OS retrieves data from Hard Disk&nbsp;in same-size blocks called pages.</span></li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4><span style="color: #000000;"><strong>Paging</strong></span></h4>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><span style="color: #000000;">It is a VMM scheme by which computer stores &amp; retrieves data from secondary storage(Hard Disk--Nowadays SSD) for use in real&nbsp;memory(RAM). </span></li><li><span style="color: #000000;">Paging let programs exceed the size of available physical memory &amp; form virtual memory.</span></li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":4} -->
<h4><span style="color: #000000;"><strong>Page Fault </strong></span></h4>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><span style="color: #000000;">A page fault occurs when a program tries to access a page that is mapped in address space but not loaded in the physical memory(RAM). </span></li><li><span style="color: #000000;">Which means that the program would have to access the paging file (which resides on the hard disk) to retrieve the desired page.</span></li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3><span style="color: #000000;"><a id="user-content-combining-page-paging--page-fault-terms" class="anchor" style="color: #000000;" href="https://github.com/VisheshPatel/OS-Concepts/blob/master/Paging%20%26%20Page%20Faults.md#combining-page-paging--page-fault-terms"></a>Combining Page, Paging &amp; Page Fault Terms</span></h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><span style="color: #000000;">When OS create virtual memory it will use paging scheme through which some block of memory is loaded in RAM &amp; rest is stored as a paging file in HDD(SSD&nbsp;in some cases)</span></li><li><span style="color: #000000;">When our <a rel="noreferrer noopener" aria-label="program(application) look for something which is not in RAM (opens in a new tab)" href="http://www.vishalchovatiya.com/how-c-program-stored-in-ram-memory/" target="_blank"><span style="color:#3914f0" class="color">program(application) look for something which is not in RAM</span></a> but stored as paging file, page fault occur.</span></li><li><span style="color: #000000;">There are many sophisticated algorithms to decrease page fault rate, like LRU, MFU, FIFO, NRU, etc</span></li></ul>
<!-- /wp:list -->