91

Can any one please make me clear what is the difference between virtual memory and swap space?

And why do we say that for a 32-bit machine the maximum virtual memory accessible is 4 GB only?

codeforester
  • 39,467
  • 16
  • 112
  • 140
algo-geeks
  • 5,280
  • 10
  • 42
  • 54
  • 1
    http://stackoverflow.com/questions/1688962/whats-the-difference-between-operating-system-swap-and-page/1689119#1689119 – starblue Feb 17 '11 at 12:45
  • @startblue how link given by you is a answer to my question.. – algo-geeks Feb 18 '11 at 10:11
  • 1
    @algo-geeks - Indeed it is not an answer, but I think it links to an answer that might be quite useful for this topic. And is written in the comment, exactly where it should be, imho. :) – Muhamed Huseinbašić Mar 06 '17 at 10:19

5 Answers5

122

There's an excellent explantation of virtual memory over on superuser.

Simply put, virtual memory is a combination of RAM and disk space that running processes can use.

Swap space is the portion of virtual memory that is on the hard disk, used when RAM is full.

As for why 32bit CPU is limited to 4gb virtual memory, it's addressed well here:

By definition, a 32-bit processor uses 32 bits to refer to the location of each byte of memory. 2^32 = 4.2 billion, which means a memory address that's 32 bits long can only refer to 4.2 billion unique locations (i.e. 4 GB).

Community
  • 1
  • 1
Viral Shah
  • 2,888
  • 2
  • 22
  • 25
  • 3
    So is *actual* RAM = virtual - swap? – user48956 Feb 27 '17 at 17:45
  • 7
    This answer is completely wrong. A person can memory map a 1GB file 100 times and use 100GB of virtual memory on a machine with just 1GB of RAM. There is no way to add RAM and disk space to get the 100GB of virtual memory that would be in use. – David Schwartz Aug 05 '17 at 04:29
  • @DavidSchwartz not really. Memory mapping a file does not load it into memory. As such, simply mmap-ing a 1GB file 100 times will not use 100GB of anything. – Felix Jun 17 '19 at 10:44
  • 1
    @Felix You are wrong. When you say it "does not load it into memory", you are using the word "memory" to mean physical memory, that is, RAM. We are talking about *virtual* memory, not physical memory. Virtual memory is generally created when and as needed, not taken from a fixed pool as physical memory is. – David Schwartz Jun 17 '19 at 16:26
  • So then it's just a reporting question? Does it ever make sense to even count how much memory is mmapped? As you will never actually use that much, be it in RAM or swap. – Felix Jun 18 '19 at 14:20
  • @Felix It seems Linux top command includes mmap into virtual memory. – jinawee Nov 01 '19 at 23:03
  • I can not agree more with @DavidSchwartz, especially in the "container world" that we live in. just spin a java docker image with `-m 100m`. sh into it and check `swapon` (usually `1GB`) and now run a very simple `HelloWorld.java` with `java -Xmx10g -Xms10g HelloWorld`. Is the swap of `1GB` + `100MB` = `10GB`? hello no! – Eugene May 18 '20 at 03:14
67

There is some confusion regarding the term Virtual Memory, and it actually refers to the following two very different concepts

  1. Using disk pages to extend the conceptual amount of physical memory a computer has - The correct term for this is actually Paging
  2. An abstraction used by various OS/CPUs to create the illusion of each process running in a separate contiguous address space.

Swap space, OTOH, is the name of the portion of disk used to store additional RAM pages when not in use.

An important realization to make is that the former is transparently possible due to the hardware and OS support of the latter.

In order to make better sense of all this, you should consider how the "Virtual Memory" (as in definition 2) is supported by the CPU and OS.

Suppose you have a 32 bit pointer (64 bit points are similar, but use slightly different mechanisms). Once "Virtual Memory" has been enabled, the processor considers this pointer to be made as three parts.

  • The highest 10 bits are a Page Directory Entry
  • The following 10 bits are a Page Table Entry
  • The last 12 bits make up the Page Offset

Now, when the CPU tries to access the contents of a pointer, it first consults the Page Directory table - a table consisting of 1024 entries (in the X86 architecture the location of which is pointed to by the CR3 register). The 10 bits Page Directory Entry is an index in this table, which points to the physical location of the Page Table. This, in turn, is another table of 1024 entries each of which is a pointer in physical memory, and several important control bits. (We'll get back to these later). Once a page has been found, the last 12 bits are used to find an address within that page.

There are many more details (TLBs, Large Pages, PAE, Selectors, Page Protection) but the short explanation above captures the gist of things.

Using this translation mechanism, an OS can use a different set of physical pages for each process, thus giving each process the illusion of having all the memory for itself (as each process gets its own Page Directory)

On top of this Virtual Memory the OS may also add the concept of Paging. One of the control bits discussed earlier allows to specify whether an entry is "Present". If it isn't present, an attempt to access that entry would result in a Page Fault exception. The OS can capture this exception and act accordingly. OSs supporting swapping/paging can thus decide to load a page from the Swap Space, fix the translation tables, and then issue the memory access again.

This is where the two terms combine, an OS supporting Virtual Memory and Paging can give processes the illusion of having more memory than actually present by paging (swapping) pages in and out of the swap area.

As to your last question (Why is it said 32 bit CPU is limited to 4GB Virtual Memory). This refers to the "Virtual Memory" of definition 2, and is an immediate result of the pointer size. If the CPU can only use 32 bit pointers, you have only 32 bit to express different addresses, this gives you 2^32 = 4GB of addressable memory.

Hope this makes things a bit clearer.

Unai Vivi
  • 3,073
  • 3
  • 30
  • 46
Yonatan
  • 840
  • 5
  • 5
15

IMHO it is terribly misleading to use the concept of swap space as equivalent to virtual memory. VM is a concept much more general than swap space. Among other things, VM allows processes to reference virtual addresses during execution, which are translated into physical addresses with the support of hardware and page tables. Thus processes do not concern about how much physical memory the system has, or where the instruction or data is actually resident in the physical memory hierarchy. VM allows this mapping. The referenced item (instruction or data) may be resident in L1, or L2, or RAM, or finally on disk, in which case it is loaded into main memory.

Swap space it is just a place on secondary memory where pages are stored when they are inactive. If there is no sufficient RAM, the OS may decide to swap-out pages of a process, to make room for other process pages. The processor never ever executes instruction or read/write data directly from swap space.

Notice that it would be possible to have swap space in a system with no VM. That is, processes that directly access physical addresses, still could have portions of it on disk.

Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
SavorALinux
  • 151
  • 1
  • 2
6

Though the thread is quite old and has already been answered. Still would like to share this link as this is the simplest explanation I have found so far. Below link has got diagrams for better visualization.

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 quiet 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.

difference-between-virtual-memory-and-swap-memory

Community
  • 1
  • 1
ram619
  • 188
  • 1
  • 9
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/13923244) – coatless Oct 08 '16 at 14:34
  • Thanks, improved the post. – ram619 Oct 08 '16 at 16:58
-1

"Virtual memory" is a generic term. In Windows, it is called as Paging or pagination. In Linux, it is called as Swap.