You are on page 1of 3

Difference between Virtual Memory and Swap Memory

Key Difference: Virtual memory is an abstraction of the main memory. It


extends the available memory of the computer by storing the inactive parts
of the content RAM on a disk. Whenever the content is required, it fetches
it back to the RAM. Swap memory or swap space is a part of the hard disk
drive that is used for virtual memory. Thus, both are also used
interchangeably.

Virtual memory is quite different from the physical memory. Programmers get
direct access to the virtual memory rather than physical memory. Virtual
memory is an abstraction of the main memory. It is used to hide the
information of the real physical memory of the system. It extends the
available memory of the computer by storing the inactive parts of the RAM's
content on a disk. When the content is required, it fetches it back to the
RAM. Virtual memory creates an illusion of a whole address space with
addresses beginning with zero. It is mainly preferred for its optimization
feature by which it reduces the space requirements. It is composed of the
available RAM and disk space.

Swap memory is generally called as swap space. Swap space refers to the
portion of the virtual memory which is reserved as a temporary storage
location. Swap space is utilized when available RAM is not able to meet
the requirement of the system’s memory. For example, in Linux memory
system, the kernel locates each page in the physical memory or in the swap
space. The kernel also maintains a table in which the information
regarding the swapped out pages and pages in physical memory is kept.
The pages that have not been accessed since a long time are sent to the
swap space area. The process is referred to as swapping out. In case the
same page is required, it is swapped in physical memory by swapping out a
different page. Thus, one can conclude that swap memory and virtual memory
are interconnected as swap memory is used for the technique of virtual
memory.

Comparison between Virtual Memory and Swap Memory:

Virtual Memory Swap Memory


Virtual memory is an
abstraction of the
Swap memory or swap
main memory. It
space is a part of
extends the available
hard disk drive that
memory of the
is used for virtual
computer by storing
Definition memory. Thus, both
the inactive parts of
are also used
the content RAM on a
interchangeably.
disk. It fetches it
back to the RAM when
the content is
required.
It extends the memory
capacity of a To store the pages
Purpose computer beyond the for temporary
one that is storage.
installed.
Memory management Area on the hard disk
Type
technique drive

Historically, swapping referred to moving from/to secondary storage a


whole program at a time, in a scheme known as roll-in/roll-out. In the
1960s, after the concept of virtual memory was introduced—in two variants,
either using segments or pages—the term swapping was applied to moving,
respectively, either segments or pages, between disk and memory. Today
with the virtual memory mostly based on pages, not segments, swapping
became a fairly close synonym of paging, although with one difference.
[dubious – discuss]

Indeed, in any context involving Linux (or other Unix systems for that
matter), paging and swapping are pretty much synonymous. Both refer to a
use of virtual memory where the data of page can either be stored in RAM
or on disk. (A page is 4kB on any device you're likely to encounter.) The
program using the memory page doesn't care or even know where the data is
stored, it just keeps using the virtual address. The kernel transfers data
between the RAM and the disk and updates the MMU (Memory Management Unit)
tables as it goes along so that the entry for the virtual address either
points to a physical page in memory, or contains a special value that
causes the processor to execute some kernel code which will load the
appropriate data from the disk.
Paging refers to this generic process. Swapping refers to the case where
the on-disk data is in a dedicated area: the swap area (a swap partition
or swap file). Paging can also be done between RAM and a file, and in this
case it's usually not referred to as swapping. For example, when you
execute a program, the code has to be loaded into memory to be executed;
if a code page needs to be evicted from RAM to make room for something
else, then there's no need to write this page onto the swap area, because
it can be loaded back from the program file. (This can be done for all
read-only data, not just program code.)

If the physical memory is (almost) full, the kernel looks for a page in
RAM (not a whole process) that hasn't been used recently. If that page
reproduces the content of a disk file (there are tables in the kernel to
indicate this), the page can be reclaimed. If not, the page is written out
to swap, then reclaimed. Either way the kernel updates the entry in the
process's virtual memory table (which becomes the MMU table while the
process executes) to mark it as not in RAM and can then reuse the physical
page for something else (a different program, or another page of the same
program).

You might also like