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

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)

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

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

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.

The output of executing that should look something like this

Now we need to make the change persist after a reboot

You need to edit your /etc/fstab file

and add the following line

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.

swappiness

To change the value

To make it persists

then add the below line to the bottom of the file

vfs_cache_pressure

To change the value

To make it persists

then add the below line to the bottom of the file

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

Hopefully this is a concise overview of how to handle your swap space.  Enjoy!

Paul H

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 *