Linux divides its physical RAM (random access memory) into chunks of memory called pages. Swapping is the process whereby a page of memory is copied to the preconfigured space on the hard disk, called swap space, to free up that page of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available.
Swap Space
Swap space will usually be a disk partition but can also be a file.
Swap space is generally recommended for users with less than 1 GB of RAM, but becomes more a matter of personal preference on systems with gratuitous amounts of physical RAM (though it is required for suspend-to-disk support).
Check Swap Status
cat /proc/swap
$ cat /proc/swap
Filename Type Size Used Priority
/dev/sda2 partition 2048276 112 -1
Shows swap partition /dev/sda2 with allocated size 2GB and 112KB used space.
swapon
swapon, swapoff – enable/disable devices and files for paging and swapping
$ swapon -s
Filename Type Size Used Priority
/var/swapfile.img file 2097144 5256 -1
Show swap file /var/swapfile.img with allocated size 2GB and with 5MB used space.
free
free – Display amount of free and used memory in the system
$ free
total used free shared buffers cached
Mem: 5986512 3210580 2775932 0 80016 1043436
-/+ buffers/cache: 2087128 3899384
Swap: 5996540 0 5996540
$ free -m
total used free shared buffers cached
Mem: 5846 3113 2732 0 78 997
-/+ buffers/cache: 2038 3808
Swap: 5855 0 5855
Here we got about 6GB allocated and unused space.
vmstat
vmstat – Report virtual memory statistics
$ vmstat 1 5
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
1 1 8392 20800 268 15568 0 2 136 309 50 137 2 2 95 1
1 1 8492 118864 268 14680 0 100 768 100 374 300 42 58 0 0
1 1 8616 27064 164 13628 0 124 640 124 344 311 44 56 0 0
1 0 9020 79260 164 12396 0 404 768 404 363 332 49 51 0 0
1 1 9464 89268 164 11328 0 444 640 444 387 320 41 59 0 0
- swpd: the amount of virtual memory used. (up to 9.5MB)
- si: Amount of memory swapped in from disk (/s). (up to 444KB)
- so: Amount of memory swapped to disk (/s). (nothing: 0KB)
Setting Up Swap Space
Normally, there are only two steps to setting up swap space, creating the partition and adding it to /etc/fstab. A typical fstab entry for a swap partition at /dev/hda6 would look like this:
/dev/hda6 swap swap defaults 0 0
The next time you reboot, the initialization scripts will activate it automatically and there’s nothing more to be done.
However, if you want to make use of it right away, you’ll need to activate it maually. As root, type:
$ mkswap -f /dev/hda6
$ swapon /dev/hda6