Tuesday, March 24, 2015

Do You Need A Swap Partition?

A question that is commonly asked when installing Linux is "Do I need a swap partition?".

In this article I am going to explain what a swap partition is used for and then I am going to let you decide whether you need on or not.

Memory is a bit like a shopping centre car park. At the beginning of the day the car park will be empty and there will be lots of spaces available. As people start arriving more

and more spaces are used up and eventually the car park will be full.

At this point there are a couple of things that can happen. You can either stop any more cars entering the car park until spaces become available or you force some of the cars to leave thereby releasing spaces.

In computing terms when you first start using your computer you should have most of your memory available. The only memory being used will be from processes required by the operating system. Every time you load an application a new process will start and a set amount of memory will be set aside for the application.

Every time you load a new application less memory will be available to run that program and eventually you will get to the point whereby there isn't enough left to run that application.

So what does the Linux do when there isn't enough memory left?

It starts killing off processes. This isn't something you really want to happen. Whilst there is a scoring mechanism for choosing which processes to kill you are basically leaving the decision up to your operating system and taking it out of your own hands.

Linux will only start killing off processes when virtual memory runs out. What is virtual memory? Virtual memory is the amount of physical RAM + any disk space set aside for paging purposes (swap).

Think of a swap partition as an overflow car park. When all of the main car parking spaces are full the overflow car park can be used for additional space. There is of course a downside to using an overflow car park. Generally the overflow car park is further away from the actual shopping centre and so drivers and passengers have to walk further to the shops which is time consuming.

You can create a swap partition which will be used by Linux to store idle processes when the physical RAM is getting low. The swap partition is basically disk space set aside on your hard drive. (Much like an overflow car park). 

It is obviously much quicker accessing RAM than files stored on your hard drive. If you find that you are constantly running out of memory and your hard drive is whirring it is likely that you are excessively using swap space.

So how badly do you need a swap partition?

If you have a computer with a small amount of memory in the first place then it is highly recommended.

As a test I set up a virtual machine with 1 gigabyte of RAM and no swap partition. I installed Peppermint Linux which uses the LXDE desktop and overall it has a low memory footprint.

The reason I used Peppermint Linux is that it comes with Chromium pre-installed and every time you open a Chromium tab a decent amount of memory is used.

I opened a tab and navigated to linux.about.com. I then opened a 2nd tab and did the same. I kept repeating this process until eventually the memory ran out. The image above shows what happened next. Chromium basically displays a message stating that the tab has stopped working and this is probably due to lack of memory.

I then set up a new virtual machine with 1 gigabyte of RAM and an 8 gigabyte swap partition. I was able to open tab after tab after tab and although the physical RAM had run low the swap space started to be used and I was able to continue opening tabs.

Clearly if you have a machine with 1 gigabyte of RAM you are more likely to require a swap partition than if you have a machine with 16 gigabytes of RAM. It is highly likely that you will never use the swap space on a machine with 8 gigabytes of RAM or more unless you do some serious number crunching or video editing.

I would however always recommending having a swap partition. Disk space is cheap. Set some of it aside as an overdraft for when you are running low on memory.

If you find that your computer is always low on memory and that you are constantly using swap space it might be time to think about upgrading the memory on your computer.

If you have already installed Linux and you didn't set up a swap partition all is not lost. It is possible instead to create a swap file which basically achieves the same goal.

Can I set aside space on my SSD for swap space?

You can set aside space on an SSD for swap space and in theory it will be much quicker to access that partition than on a traditional hard drive. SSDs do have a limited life span and can only handle a certain number of reads and writes. To put things into perspective that number is actually very high and your SSD will probably outlast the life of your computer.

Remember swap space is supposed to be an overflow buffer and not used consistently. As mentioned before if you find that you are constantly using the swap partition consider upgrading the memory.

No comments:

Post a Comment