There are two main storage memories in the computer system. One is RAM (Random access memory) also known as main memory other is secondary memory also known as a hard drive.
All the computer programs are stored in the main memory while running. The CPU (central processing unit) or processor get programs data from the main memory. The main memory is a type of volatile memory that means all the data from the main memory is removed after the computer shut down.
The speed of a computer depends upon the size of the main memory (RAM). Nowadays some programs are large than the size of RAM so these programs need to be executed even if RAM is small in size. To overcome this issue a virtual memory is used.
Virtual memory is a temporary file that is created on the hard drive. Virtual memory stores processes which are not needed for some time in the RAM. Suppose RAM is 500 MB and 490 MB is used and 10 MB is left. So 490 MB of virtual memory is created on a hard drive.
Every process is divided into pages and stored on the secondary storage. Suppose there are two processes A and B and 4 pages of A are created and 3 pages of B are created. Process A is residing in the main memory and process B is residing in virtual memory. Suppose process A doesn’t need to be executed for some time so it is moved to virtual memory. The moving of process A from main memory to secondary memory is known as swapping out of the process. While process B is to be executed so its 3 pages will be moved from virtual memory to main memory and this movement of process B is known as a swap-in.
The speed of RAM is in nanoseconds while the speed of the hard drive is in milliseconds. If more processes are moving in and out of secondary memory so computer becomes slow.
There also comes a time when switching processes are happening more often than doing actual processing so this issue is known as thrashing.
There is also a page table created that stored frames of all the processes and the address of the process in the main memory. The frame is actually the process number. If the address of the process is not found in the page table then the operating system gets the control and fetches that process from secondary memory and moves it to the main memory. Then OS updated the address of the process in the page table and gives control back to the user.