Linux - Swap

Toverify the amount of swap space available:

cat /proc/meminfo

You might want to have a swap twice as large as your physical (silicon) memory, but not less than 64 MB.

You can have several swap partitions.

To create and enable a swap partition:

  1. Create the partition of the proper size using fdisk (partition type 82, "Linux swap")
  2. Format the partition checking for bad blocks, for example: mkswap -c /dev/hda4
  3. Enable the swap: swapon /dev/hda4
  4. To have the swap enabled automatically at bootup, you have to include the appropriate entry into the file /etc/fstab: /dev/hda4 swap swap defaults 0 0

To disable the swap:

swapoff /dev/hda4

Swapping to files is usually slower than swapping to a raw partition, so this is not the recommended permanent swapping technique. Creating a swap file, however, can be a quick fix if you temporarily need more swap space. You can have up to 8 swap files, each with size of up to 16 MB. Here are the steps for making a swap file:

  1. Create a file with the size of your swap file: dd if=/dev/zero of=/swapfile bs=1024 count=8192 (This physically creates the swap file /swapfile, the block size is 1024 bytes, the file contains 8192 blocks, the total size is about 8 MB)
  2. Set up the file with the command: mkswap /swapfile 8192
  3. Force writing the buffer cache to disk by issuing the command: sync
  4. Enable the swap with the command: swapon /swapfile
  5. When you are done using the swap file, you can turn it off: swapoff /swapfile

How to enable swap?

swapon -a

How to disable swap?

swapoff -a
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License