Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

As far as I understand paging and swapping, they're completely different concepts. While swapping means, that a process is either completely in physical memory or on the hard drive, with paging parts of a process can be in physical memory and other parts can be on the hard drive.

But why does linux need a swap partition then? If the physical memory is full, some processes will be outsourced to the hard drive and a new process will be mapped from virtual memory to physical memory.

I just don't get why one needs a swap partition (or swapping in general) then?

Or is this just a matter of terminology and swap partition == virtual memory?

share|improve this question
2  
Beware that "virtual memory" in computer architecture is a technique, whereas MS Windows (incorrectly IMO) defines it as "Virtual memory is storage space on your comptuer's hard disk that Windows uses in conjunction with random access memory (RAM)." I.E. google windows "virtual memory" –  sawdust Mar 27 '13 at 0:23

4 Answers 4

Yes it is just a matter of terminology, in many cases a swap partition is used as virtual memory.

The reason UNIX and UNIX-like systems prefer swap partitions to page-files is that they can be contiguous which results in lower seek times compared to a page-file which may be fragmented.

share|improve this answer
5  
And this way it's also easier to share a single swap partition between Linux installations. –  oKtosiTe Mar 26 '13 at 14:15
2  
usually a pagefile is completely created on the first boot, so it won't be fragmented (well.. there's still a few possibilities..) –  AndreaCi Mar 26 '13 at 14:18
11  
The very earliest UNIXes only had swapping, not paging, and could only swap to a dedicated partition. Paging was implemented as soon as the hardware supported it, but the name "swap partition" stuck. Paging to a file is more recent and has higher OS overhead as well as risking fragmentation, so it's still discouraged. –  Zack Mar 26 '13 at 14:53
    
"swap partition is used as virtual memory." -- Only Microsoft Windows defines secondary storage (e.g. page file on disk) as "virtual memory". But even they are trying to move away from this usage. Try googling windows "virtual memory" and the synopsis for the first result ("Virtual memory is storage space ...") does not match the page contents. A correct statement would be a "swap partition is used by virtual memory." –  sawdust Mar 28 '13 at 0:18

I don't know where you got the notion that “swapping means, that a process is either completely in physical memory or on the hard drive”. That meaning has not been in use for a few decades. Quoting Wikipedia:

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

share|improve this answer

The virtual memory/paging facility lets a kernel "virtualize" memory to userspace processes. The kernel can take pages from physical memory, and arrange them through paging so they appear contiguous to a userspace process.

A limit can be set on a userspace process's memory and if the process goes beyond it a "page fault" occurs, which causes a CPU exception which bounces back to the kernel. This prevents the userspace program from messing with memory allocated to the kernel or other programs, without the kernel's permission.

Typically userspace programs ask the kernel to extend this limit via well defined interfaces (called by the C functions malloc() and free() for example.). The kernel is responsible for keeping track of how much and what memory a program is allocated.

This "page fault" mechanism can also let the kernel swap the page the process was trying to access with one from disk, if the kernel is able to overprovision memory (and both Windows and Linux support this) hence why it is called swapping. If the memory access was indeed invalid (i.e. the process is trying to access memory it didn't ask for first) then typically the process will be killed with a SIGSEGV.

So "swapping" is an additional feature (in Linux you can actually disable it entirely if you want) that depends on virtual memory/paging, but isn't required just because a CPU has virtual memory/paging. The concepts are not the same but swapping depends on paging/virtual memory to exist.


Also, after more closely reading your question, "paging" is sometimes uses as a synomym for "swapping" - but I've never heard of "swapping" meaning the whole process's memory is swapped out vs. "paging" meaning only part of it is swapped out.

But why does linux need a swap partition then? If the physical memory is full, some processes will be outsourced to the hard drive and a new process will be mapped from virtual memory to physical memory.

"Virtual memory" is physical memory, just "remapped." The MMU hardware cannot directly map to any storage device. The MMU can throw a fault that tells the kernel a process tried to access memory it shouldn't have - and the kernel can use this mechanism to see that a process wants something back from disk that it thought was in memory and then do the "swap". The point being it's the operating system that decides to save pages to disk so it can use those pages for other processes, not the hardware.

share|improve this answer

In general swap partition is not equal to virtual memory.

Processes can need more memory than actual physical memory, so OS developers decided to assume there is more memory in the system which is called "virtual memory".

This virtual memory basically is physical memory and a part of the disk. This part of the disk is called "swap" in Linux.

Also developers proposed that usage of the part of the virtual memory that is located on the hard disk must be as low as possible. For the sake of it, all virtual memory was divide to small portions which are called "pages". A lot of pages are used in low rate, these pages must be written out to the part of the virtual memory on the hard disk. This operation is called "swap out". The OS must keep track of which pages are not in the physical memory to find them when needed. Page fault happens when a program wants to write/read a part of the memory that is swapped out.

To answer your question: Linux needs a swap partition to swap out some pages of memory and you can see a statistic of virtual memory usage with vmstat:

procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r  b   swpd   free   buff  cache    si   so    bi    bo   in   cs us sy id wa
2  0  35424 524560 134164 1858728    0    0    13    11   55   42  5  1 94  0
0  0  35424 526720 134164 1857872    0    0     0     0 2774 5224  7  2 91  0
1  0  35424 516328 134172 1870116    0    0     0     6 3464 6561 13  3 84  0
0  0  35424 522992 134212 1862676    0    0     0   125 4135 7135 12  4 84  1

The 'swap' column shows swap out and in statistic. Also this link explains virtual memory and usage of vmstat as well.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.