Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between "virtual memory" and "swap space"?

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?

like image 435
algo-geeks Avatar asked Feb 11 '11 14:02

algo-geeks


People also ask

What is virtual memory and swap memory?

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. It fetches it back to the RAM when the content is required. Swap memory or swap space is a part of hard disk drive that is used for virtual memory.

What is swap space in operating system?

3. Introduction to Swap Space Like virtual memory, swap space is a secondary memory. It’s used by the OS when there is no physical memory available for further execution of the processes. If the OS faces a situation when it needs memory, but the RAM is full, it moves the inactive pages from RAM to swap memory.

How does virtual memory extend the available memory of the computer?

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.

What is swappable space?

Swap space is a space on a hard disk that is a substitute for physical memory. It is used as virtual memory which contains process memory images. Whenever our computer runs short of physical memory it uses its virtual memory and stores information in memory on disk.


3 Answers

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

like image 189
Viral Shah Avatar answered Oct 20 '22 04:10

Viral Shah


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.

like image 21
Yonatan Avatar answered Oct 20 '22 03:10

Yonatan


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.

like image 39
SavorALinux Avatar answered Oct 20 '22 04:10

SavorALinux