Swap Space in Linux
Posted by Ryan Coyner on June 2, 2008 under Linux - 3 Comments
I was installing Arch Linux on my laptop a couple of weeks ago and found myself staring at the cfdisk interface trying to decide the amount of memory I should allocate to my swap partition. I spent about a hour on Google looking for a reasonable answer, and most websites recommended that a swap partition should have twice as much memory as the amount of RAM on your computer. Unfortunately, all the websites failed to explain why that much memory should be allocated to the swap partition. I continued my research and not surprisingly, I found very few resources that explained in detail what a swap space is and how swapping works in Linux.
Swap Space
So what is a swap space? Swap space is a section of secondary memory (e.g. hard drives/magnetic disks) used for allocating pages (i.e. blocks of virtual memory/data) that are not present in physical memory (i.e. RAM). The swap space is usually a dedicated partition on a disk, although a swap file or a combination of the two can also be used. Although you could rely solely on a swap file, Arch Linux (and probably most other Linux distributions) requires you to define a swap partition during the installation process. This is because hibernation (suspend-to-disk) in the 2.6.x kernel is implemented using swsusp, which is only able to hibernate using a swap partition and not a swap file. In Linux, the swap space is used when all the physical memory on your computer is reserved by running processes yet an additional demand for memory resources exist. In such scenarios where the physical memory is at full capacity, the kernel allocates inactive pages in the physical memory to the swap space. This creates additional space in the physical memory to meet the additional demand. When the pages that are in the swap space are needed by the physical memory again, they are swapped backed into the physical memory. These operations are called swapping out and swapping in, respectively.
Swapping
Swapping is a lot like Paging. Their functionality are similar but there is a difference. Paging is a process where the kernel retrieves pages from the disk to physical memory, or transfers pages from physical memory to the disk. These operations are called paging in and paging out, respectively. Paging takes place when you launch a process (e.g. Firefox); the kernel retrieves the data necessary to run the process from the disk and puts them into the physical memory for the CPU to be able to use. On the other hand, swapping in and swapping out only refer to pages being transferred between the physical memory and the dedicated swap space (or swap file) on the disk.
As I mentioned above, swapping most commonly takes place when your computer is requesting for more memory than there are physically available. The kernel then swaps out less used pages and allocates memory to the process that is requesting for more memory. Since most computers today have a lot of physical memory, swapping only tends to take place when you are simultaneously running several memory-intensive processes.
Swap Space is Important
Swap space is necessary for two important reasons. If you do not have any swap space and start a new process when your physical memory is at full capacity, you will be out of memory. Linux handles out of memory by killing low-priority processes until enough physical memory is released for the new process to run. If you have a swap space, you will be swapping instead. However, even if you have a swap space, you will still be out of memory if both the swap space and physical memory reaches full capacity.
Swap space also helps conserve physical memory. When a process initializes, it reserves a chunk of physical memory to use. While a process is running, it uses this reserved memory and more is allocated by the kernel if the process exceeds the reserved amount. However, most applications never surpass the initial reserved amount of memory. This is problematic because the kernel calculates available free memory based on the amount of unreserved memory. Thus, there usually exists a chunk of memory in each process that is not being used but cannot be used by any other processes because it is reserved specifically for the process that reserved that chunk of memory. If you have swap space, this unused chunk can be swapped out when the physical memory is at full capacity. Once all unused chunks are swapped out and the physical memory is still at full capacity, the kernel will swap out pages used by a process only during its initialization and never used again. After that, the kernel will begin swapping out inactive pages. If any of those pages are ever used again, they are swapped back in.
You can view statistics of the virtual memory and how much swapping takes place with the vmstat command:
$ vmstat -s
1034200 total memory
824108 used memory
531216 active memory
244116 inactive memory
210092 free memory
133120 buffer memory
312504 swap cache
1951856 total swap
0 used swap
1951856 free swap
816196 non-nice user cpu ticks
0 nice user cpu ticks
67224 system cpu ticks
6802786 idle cpu ticks
7373 IO-wait cpu ticks
3129 IRQ cpu ticks
1240 softirq cpu ticks
0 stolen cpu ticks
248480 pages paged in
1033015 pages paged out
0 pages swapped in
0 pages swapped out
10249297 interrupts
25564788 CPU context switches
1212406629 boot time
6805 forks
Paging in and paging out occurs frequently, and swapping in and swapping out occurs only under dire circumstances.
So how big should the swap partition be?
It depends. If you are planning to run several memory intensive processes, you would want a liberal amount of swap space. That being said, it is highly advised to invest in more RAM instead, as disk access is several orders of magnitude slower than physical memory access. Your computer will be crawling if you substitute physical memory with swap space. For most computers, you should at the very least have a swap space as large as the amount of RAM your computer has. This is because hibernation swaps out all the pages in the physical memory to the swap space before powering off the computer. That is why most resources recommend you to allocate twice the amount of RAM for a swap partition.

3 Comments
harry October 2, 2008 at 04:57
okay... now for a situation i think is normal:
i have my mythtv backend at home with storage. it has 2GB of ram, it uses 1GB for buffers and swap, it has allmost allways 50-80MB of physical ram free, i NEVER hibernate/suspend to disk since it's a server and it uses (after 30 days uptime) about 10MB of swap. total swapspace is 2GB... why would I need this much swap??? 128MB or 512MB would be more than enough... same goes for my laptop, i never use hibernate/... i never use more than 10-50MB of swap... since i have a "small" disk there, and 4GB of ram, isn't it enough to have about 512MB swap??? and if it will use 512MB, my laptop will be so slow, that it would mean there is something wrong, since i don't use memory intensive applications...
grtzzzz...
Kyle Keen October 2, 2008 at 11:57
Arch does not "requires you to define a swap partition during the installation process".
True, most distros do, but this is one of the reasons I use Arch. All of my laptops for the past four years have used flash drives in various forms, all of them have run Arch, and not one has a swap partition.
Also, gurus have advocated "twice the RAM" long before any computer could hibernate. IIRC, this was so in the event of a failure, a full memory dump could be saved to the swap.
harry October 3, 2008 at 03:17
i usually have "the amount of ram" in swap... never had problems, and if my system actually NEEDS swap, i buy more ram ;)
tnx for the investigation tough ;)