Swap partition

From HPCWIKI
Jump to navigation Jump to search

What is swap ?

Swap partition acts as a disk memory in Linux when the RAM is exhausted, your Linux system uses part of the hard disk memory and allocates it to the running application.

Although Linux can run without swap partition if the system has plenty of RAM, a little amount of swap is always advisable. In fact, a distribution like Ubuntu automatically creates a swap file of 2 GB in size. This should give you an indication that having some swap space is recommended.

How much of swap do we need to set ?[1]

There are many discussion on what would be idle size of swap, RedHat recommend a swap size of 20% of RAM for modern systems (i.e. 4GB or higher RAM). CentOS has a different recommendation for the swap partition size. It suggests swap size to be:

  • Twice the size of RAM if RAM is less than 2 GB
  • Size of RAM + 2 GB if RAM size is more than 2 GB i.e. 5GB of swap for 3GB of RAM

Ubuntu has an entirely different perspective on the swap size as it takes hibernation into consideration. If you need hibernation, a swap of the size of RAM becomes necessary for Ubuntu.

Otherwise, it recommends:

  • If RAM is less than 1 GB, swap size should be at least the size of RAM and at most double the size of RAM
  • If RAM is more than 1 GB, swap size should be at least equal to the square root of the RAM size and at most double the size of RAM
  • If hibernation is used, swap size should be equal to size of RAM plus the square root of the RAM size

Check and create swap partition

// Check SWAP partition and size
#swapon -s

// Create SWAP 2GB partition example
# fallocate -l 2g /swap
# chmod 0600 /swap
# mkswap /swap
# swapon /swap
# echo '/swap    none swap defaults 0 0' | tee -a /etc/fstab
# free -m

References