Heya, Thanks for visiting!

Swap File Guide (Ubuntu)

  • server
  • ubuntu
  • memory

Here is a basic howto guide on setting up and managing a swap file. I am using Ubuntu 12.04 but I am sure it applies to other versions and Linux distros. I recently had to figure out how to increase the size of an existing swap file so I thought I would throw together a guide(see section below "Create Swap file` if you need to do this).

A swap file(called a page file in Windows) is a way to increase memory/RAM capacity without upgrading hardware (virtual memory). It is simply a file on your hard drive. When your physical sticks of RAM get full, the system will copy over to the swap file to avoid a crash or lose data. Swap files will be slower at reading and writing because they reside on your hard drive but is better than a complete failure.

See table of useful commands (cheatsheet) when dealing with swap files at the bottom of this article.

Create a swap file

Create a file for swapping

You need to create a big file the size of your swap. Use dd or fallocate.

Create a 512MB or .5GB swap file: sudo dd if=/dev/zero of=/swapfile bs=1024 count=524288
Create a 1024MB or 1GB swap file: sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576

  • Size is determined by: bs*count in bytes
  • if=/dev/zero makes it read from that file. /dev/zero returns as many null characters that are read.
  • of=/swapfile determines the name
  • bs is the number of bytes in a chunk
  • count is the number of chunks

Prepare the swap file

Run: sudo mkswap /swapfile

Activate the swap file

Run: sudo swapon /swapfile

Make the swap file permanent by adding it the the fstab

  • Open /etc/fstab. You could use: sudo nano /etc/fstab
  • Add this line to the end: /swapfile none swap sw 0 0

If you want to learn more about the fstab syntax/configuration, see this fstab Ubuntu doc

Set the permissions on the swap file to 0600:

This will only allow the owner(root) to read and write to the file.

  • sudo chown root:root /swapfile
  • sudo chmod 0600 /swapfile

Set swappiness to 10

Swappiness is a value 0 to 100. A value of 0 means that the swap file will be avoided until actually necessary (run out of memory). A value of 100 will put stuff into the swap file as soon as possible. The default is 60. You can think of the number as the percentage left of actual RAM when the system starts copying into the swap file.

A swappiness value of 10 will make it act as an emergency airbag to avoid out of memory errors.

Run both commands:

  • echo 10 | sudo tee /proc/sys/vm/swappiness OR sudo sysctl vm.swappiness=10
  • echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf

The first one modifies the swapiness variable in the virtual memory(vm) process(not permanent). And the second makes it permanent by adding it to the conf which gets loaded in at boot time.

You can see how tee works here. It just overwrites the file and displays what is written. The -a parameter appends the text instead.

Alternatively you can just:

  • Open /etc/sysctl.conf. You could use sudo nano /etc/sysctl.conf
  • Find vm.swappiness. If it doesn't exist, add it to bottom of the file: vm.swappiness=10
  • Change value to 10 and save. If using nano: press Ctrl+x to exit, then press y to save.

You can check your swappiness value using this command: cat /proc/sys/vm/swappiness

Verify it is working:

Run sudo swapon -s to see a summary of your swap file setup. It should be listed.

Also if you run free -m, it should show the split in usage between mem and swap.

Resize existing swap file (increase swap size/space)

Just create a new swapfile (follow instructions above), then deactivate the old one. Don't forget to update the fstab.

To deactivate a swapfile: sudo swapoff /swapfile
To delete the swapfile: sudo rm /swapfile

Other References:

Basic Command cheatsheet:

sudo swapon -sList swap files
free -mShow memory usage breakdown
cat /proc/sys/vm/swappinessCheck swappiness value
sudo swapon -aTurn on all swap files
sudo swapon /swapfileTurn on swap file specified
sudo swapoff -aTurn off all swap files
sudo swapoff /swapfileTurn off swap file specified