How To Add Swap Space On Ubuntu

1 mainWhenever you are building an application that’s memory intensive, you are bound to run into memory issues. Those out of memory errors are painful to deal with, especially when they happen during production. Before putting your code on your server, you need to make sure that it can handle the application’s memory requirements. But even if you are careful, something might still go wrong and you might end up running into memory issues. One of the easiest ways to deal with this is by adding some swap space. Now how will it help our case? How can we use it on Ubuntu?  

What exactly is “swap” space?

When you run an application, it uses the system’s RAM to do all the operations. So when you run out of memory, it actually means that you ran out of memory in your RAM. In order to solve this, you need some extra memory. As we know, every system has a hard drive associated with it. So may be we can borrow some space from there! This is where swap space comes into picture.

Swap space is a portion of the hard drive that the system uses as a temporary storage when it runs out of RAM. If we don’t have swap space, then the system will crash because there’s no space left in the RAM. Adding swap space gives us the ability to increase the amount of memory we can work with.

Now, as you know, there are no free lunches in this world. So there’s actually a catch here! The hard disk is slower than RAM. What this means is that when we access the hard disk to read and write data, it will be slower than it would have been with the RAM. So it’s not advisable to use this. We just need to have swap space as a backup plan in case the system’s RAM gets depleted.

Let’s get our swap on

Now that we know what it is, let’s go ahead and add some swap space to our system. If you are not sure whether your system already has swap space configured, just run the following command in your terminal:

$ sudo swapon -s

If you see a table with only headers and no numbers, it means that you don’t have any swap space allocated. You can also check for swap space using:

$ free -m

The last line in the output should indicate the swap space. If it shows “0”, it means there’s no swap space allocated. Let’s see how much memory we have in our hard disk:

$ df -h

In the first line, the column “Avail” should indicate the amount of memory you have at your disposal. We can some of this to allocate swap space.

Assigning swap space

We’ll use the “fallocate” program to create a file of preallocated size that will be used as the swap space. Let’s say our system has 4GB RAM and we want to add 1GB of swap space. In order to do that, let’s create a swap file by running the following command:

$ sudo fallocate -l 1G /mySwapSpace

Let’s verify that the allocation was successful by running the following command:

$ ls -lh /mySwapSpace

You should see the file listed with the right amount of memory.

Let’s enable the swap file

Now that the file is created, we need to tell our system that it can use this file for swap. Before we do that, let’s lock the permissions for the file:

$ sudo chmod 600 /mySwapSpace

Now we need to tell our system that it can use this file for swap:

$ sudo mkswap /mySwapSpace

The system now knows that it can use this file for swap. We now need to enable this file by running the following command:

$ sudo swapon /mySwapSpace

This file is now ready to be used as swap. Let’s check if swap space is available:

$ sudo swapon -s

Remember how this command showed a table earlier with headers and no numbers? Well, now you should see the same table headers, but with numbers this time. You will see 1GB in the “Size” column. You can verify that using:

$ free -m

In the last line, you should see the amount of swap space allocated.

You are all set! Your system can now handle more memory intensive applications. But remember, whenever you use swap space, your system will be really slow. So if you’ve allocated swap space and your system is taking forever to return the output, check the swap space using “free -m”. If the system is using swap space, you’ll see the last line in the output indicating the amount of swap space being used. If possible, always design your application to make sure you never need swap space. Just have the swap space as a backup, because a slow system is better than a crashy system!

———————————————————————————————————

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s