+4 votes
253 views
How to create Swap in Ubuntu and what it is for

in Guides by (552k points)
reopened | 253 views

1 Answer

+5 votes
Best answer

What is a Swap partition and what is it for?
I need Swap and what size do I create the partition?
How to create Swap in Ubuntu?
Making the Swap permanent
Final tips
Swappiness
Swap Permissions

The objective of this article is to learn to add or create a Swap partition in Ubuntu . This tutorial has been tested on Ubuntu 12.04 and Ubuntu 12.10 versions. During the process of creating the Swap partition we will only use an SSH shell.

image

What is a Swap partition and what is it for?

We will not go into great technical details, we will define it in a simple way, it is virtual memory . It is called virtual because the hard disk is used to create a swap file and RAM is not used. This allows us to have extra memory, but this memory has a lower performance than RAM, since when using the hard disk the process of reading / writing / access is slower.

I need Swap and what size do I create the partition?

The first thing is to consider if we need a Swap partition, I would recommend it if our system has a low amount of RAM (meaning lower than 2GB). Another factor to consider is the use of RAM, if we can predict that the RAM of our operating system is going to fall short it is best to create the Swap. To put a real situation, on the DigitalOcean VPS servers of 512MB of RAM my recommendation is to create a Swap partition. This company specifically uses SSDs so Swap performance is very acceptable.

Once we have decided whether or not we need the Swap partition, we are asked the question of what size we should create this partition. This is not a simple question to answer, since it will depend on the use we are going to give it. Years ago there was an unwritten law that the Swap should be twice the size of the amount of RAM in our system. For example, if our PC had 2GB of RAM, the Swap must be 4GB. This, in my humble opinion, has become outdated and my recommendation is to create a Swap of size according to our needs. To give a real example, on the VPS server I was talking about before 512MB of RAM, I create a Swap partition of another 512MB since in my opinion the use of the Swap has to be punctual and not something that happens constantly . If the use of Swap is high and constant, I think the best option is to increase the amount of RAM directly or we will be facing a system with a lower performance .

How to create Swap in Ubuntu?

The first thing is to verify that there is no Swap partition in our system, for this we use the command:
sudo swapon -s

When executing it we should be shown an empty list with the following:

Filename Type Size Used Priority

The next step is to verify that we have enough free space on the hard disk, for this we execute the following command:
df

And something like:

Filesystem 1K-blocks Used Available Use% Mounted on
/ dev / sda 20907056 1437188 18421292 8% /
udev 121588 4 121584 1% / dev
tmpfs 49752 208 49544 1% / run
none 5120 0 5120 0% / run / lock
none 124372 0 124372 0% / run / shm

In this list we can see the use percentage (Use%) and that is 8% in /dev/sda so we can create our 512MB Swap with the following command:
sudo dd if=/dev/zero of=/swapfile bs=1024 count=512k

In that command "swapfile" is the name of our Swap file, I have used "swapfile" as the name. The next command to execute is:
sudo mkswap /swapfile

Something similar to:

Setting up swapspace version 1, size = 262140 KiB
no label, UUID = 103c4545-5fc5-47f3-a8b3-dfbdb64fd7eb

Finally, we activate the Swap with the command:
sudo swapon /swapfile

We can verify that the Swap partition has been created and activated with the command:
sudo swapon -s

This Swap will be active until we restart the operating system, once restarted it will be lost. Sometimes this is what we want but in other cases we are interested that the Swap partition is permanent, in this case we proceed as indicated in the following section.

Making the Swap permanent

In order not to lose the Swap after a restart, we must edit the fstab file with the nano editor as follows:
sudo nano /etc/fstab

And once in the file we add the following line:
/swapfile none swap sw 0 0

Remember that to save with nano, use the keyboard combination Ctrl+O and to exit Ctrl+X

Final tips

Swappiness

When creating Swap we can define the swappiness property, it defines the balance that the system must do between using RAM or using Swap memory. This property is defined with values "‹"‹between 0 and 100. By default it usually uses the value 60. Let's analyze what would happen with different swappiness values:

  • Swappiness value 0 : The operating system would try to avoid using the Swap and only use it to avoid insufficient memory errors (out of memory errors).
  • Swappiness value 60 : default value and one could say that the Swap will be used in one of every six operations.
  • Swappiness value 100 : the operating system will make aggressive use of the Swap, giving full priority to the use of Swap. It is not highly recommended to use this value since only performance drops are achieved.

To adjust the swappiness with the value 0 we must use the following command:
echo 0 > /proc/sys/vm/swappiness

Swap Permissions

A good practice (especially in systems with several users) is to give the correct permissions to the Swap file so that it cannot be read / modified deliberately by the users. It is good practice to execute the following two commands to give the appropriate permissions:
sudo chown root:root /swapfile
sudo chmod 0600 /swapfile

And this ends the guide on how to create Swap in Ubuntu, I hope it has helped you.


by (3.5m points)

Related questions

+4 votes
1 answer
+3 votes
1 answer
+4 votes
1 answer
+4 votes
1 answer
+5 votes
1 answer
Sponsored articles cost $40 per post. You can contact us via Feedback

Most popular questions within the last 30 days

10,659 questions
10,791 answers
510 comments
3 users