I have been working with Linux for quite sometime and when we say virtual memory are we referring to swap space? Say for instance in the top command we have a separate column for VIRT(virtual memory), does it refer to the swap space of the linux machine.I want to know whether virtual memory and swap space are the same thing? can someone help me clear out this confusion?
top manpage distinguishes them as follows:
For our purposes there are three types of memory, and one is optional. First is physical memory, a limited resource where code and data must reside when executed or referenced. Next is the optional swap file, where modified (dirty) memory can be saved and later retrieved if too many demands are made on physical memory. Lastly we have virtual memory, a nearly unlimited resource serving the following goals:
- abstraction, free from physical memory addresses/limits
- isolation, every process in a separate address space
- sharing, a single mapping can serve multiple needs
- flexibility, assign a virtual address to a file
VIRT column in
top shows how much address space a process has allocated. Much of that is typically unused. When it is used, the kernel has to make physical memory available to the process. Ideally, this physical memory can be taken from the pool of free memory, and nothing else is affected. If however there isn’t enough free physical memory, the kernel has to get rid of some other data in memory, and that is where swap space comes in: one of the ways the kernel can make memory available is by writing its contents to swap.
Virtual memory is a somewhat abstract concept: as represented by the
VIRT column in
top, it’s all the memory a process has said it could potentially use. Swap space is one way of helping the kernel make memory available when a process needs to use it.