Menu

Memory Management

Virtual Memory

Virtual memory is a technique the operating system uses when there is not enough free main memory (RAM) for all the processes that need to run. It lets the computer carry on working by treating part of secondary storage as an overflow area for memory.

Swapping processes out

RAM can only hold so much at once. When it runs short, the operating system looks for processes that are sitting in RAM but are not being used by the CPU at that moment, and moves them onto secondary storage such as a solid-state drive or hard disk. Their instructions and data are written to a reserved area of the drive, and the space they were taking in RAM is freed.

That freed space is then given to an active process, so the program you are actually using keeps running. For example, you might be editing a long video while you look something up in a browser. While you are in the browser, the video editor's data is swapped out to the SSD; when you click back on it, it is swapped into RAM again.

Diagram of virtual memory: an idle video editor is swapped out from RAM to a reserved area of the SSD or hard disk, freeing space for the browser in use, and swapped back in when needed again.

Swapping processes back in

When a process stored on the drive is needed again, the operating system copies it back into main memory, moving something else out to make room. Swapping a process out never closes, deletes or uninstalls the program โ€” it is only parked, and it continues from where it stopped.

Why performance drops

Secondary storage is much slower to read and write than RAM, so every swap costs time. A computer relying heavily on virtual memory feels sluggish: programs respond slowly and switching between them causes noticeable pauses.

StorageSpeedJob in virtual memory
RAMFastHolds the programs and data in use, which the CPU reads from and writes to directly
Secondary storageSlowTemporarily holds processes that have been swapped out

The gain is that virtual memory extends the usable memory of the computer without anyone buying and fitting extra RAM chips, so programs can still run when RAM is short. It uses the computer's own drive, not an internet service.

Practise this