Adding swap space Ubuntu 14 04 and configuring

I have been having a bit of a fight with adding swap space Ubuntu 14 04.

Again there are already many posts about this in ‘blog land’ but I found some went into too much detail and only confused matters, and some didn’t cover all the areas I needed.

The main issues I have been having have been around adding swap and then losing it when I reboot my system and also the performance of the swap space as the system seems to want to swap everything out.

The main points I am going to cover in this blog are

  • Checking current swap details
  • Adding Swap space (2 different methods)
  • Making the swap change persist (don’t lose your swap settings after a reboot)
  • Configure the 2 of the main swap performance parameters (swappiness and vfs_cache_pressure)

Check your current swap space

sudo swapon -s
free -m

These are the two main commands you will need the first one simply checks your swap information (the columns are fairly self-explanatory, the second will check all your used and free memory)

The output should look something similar to this (if you don’t have any swap currently)

bitnami@gothailand:~$ sudo swapon -s
Filename Type Size Used Priority
bitnami@gothailand:~$ free -m
total used free shared buffers cached
Mem: 2001 1913 88 168 105 479
-/+ buffers/cache: 1327 673
Swap: 0 0 0

Adding swap space

There are two methods here a slow (old way) and a fast (new way) approach.

Very important here you DO NOT overwrite an existing swapfile you identify above.

Firstly the old way

sudo dd if=/dev/zero of=/mnt/swapfile bs=1G count=2

This command creates a 2GB blank file to be used for swap in /mnt/swapfile, because bs=1G is a block size of 1GB, count=2 means it creates 2 of them.  2x1GB = 2GB

Secondly the new fast way and probably the way you should be doing it

sudo fallocate -l 2G /mnt/swapfile

Now whichever method you chose you need to enable the swap

So you need to change the swap permission, enable it and verify it has taken effect using the below commands.

# Change permissions
sudo chmod 600 /mnt/swapfile
# Enable swap
sudo mkswap /mnt/swapfile
sudo swapon /mnt/swapfile
# Check swap
sudo swapon -s
free -m

The output of executing that should look something like this

bitnami@gothailand:~$ # Change permissions
bitnami@gothailand:~$ sudo chmod 600 /mnt/swapfile
bitnami@gothailand:~$ # Enable swap
bitnami@gothailand:~$ sudo mkswap /mnt/swapfile
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=a6914248-84a2-48a8-939c-75778af4ec26
bitnami@gothailand:~$ sudo swapon /mnt/swapfile
bitnami@gothailand:~$ # Check swap
bitnami@gothailand:~$ sudo swapon -s
Filename                                Type            Size    Used    Priority
/mnt/swapfile                           file            2097148 0       -1
bitnami@gothailand:~$ free -m
 total used free shared buffers cached
Mem: 2001 1913 87 168 107 478
-/+ buffers/cache: 1328 672
Swap: 2047 0 2047

Now we need to make the change persist after a reboot

You need to edit your /etc/fstab file

sudo nano /etc/fstab

and add the following line

/swapfile   none    swap    sw    0   0

Configure swap performance parameters

There are two main swap performance parameters I like to ‘tweak’ to give me added performance (swappiness and vfs_cache_pressure).

I won’t go into huge detail about the tech side but give an overview.

swappiness is a percentage about how likely the OS will swap out memory to disk, the closer to 0 the less likely it will, towards 100 it will try and swap everything out.  Swapping to disk is obviously a performance hit so I like to reduce this a lot (the default is 60%)

vfs_cache_pressure is the percentage of how likely it is to swap out information about the filesystem over other data in memory.  This is very costly and happens frequently.  The default is 100% so I like to reduce this a lot too.


To change the value

sudo sysctl vm.swappiness=10

To make it persists

sudo vi /etc/sysctl.conf

then add the below line to the bottom of the file



To change the value

sudo sysctl vm.vfs_cache_pressure=60

To make it persists

sudo vi /etc/sysctl.conf

then add the below line to the bottom of the file


A final check of your memory now should reveal you have swap!

Hope this helps, please share or comment if you found it useful!


IT consultant with 20+ years experience specialising in Oracle Database, Oracle Business Intelligence, Web/Mobile development, Application Express development, cloud technology and more

Leave a Reply

Your email address will not be published. Required fields are marked *