How to create a swapfile on your server in Ubuntu and deal with increasing memory load

Jonas Maro
4 min readFeb 14, 2022
Unsplash

An easy way to increase the responsiveness of your server and avoid problems with RAM consumption due to a high number of requests is to add a swapfile. The swapfile is a file, large in size and reserved on the hard disk, where the operating system can temporarily store data that it cannot retain in main memory or RAM due to lack of space in it. At first glance it is like using a part of your hard drive as if it were RAM.

This allows you to increase the memory capacity of your server to be able to cope with a greater workload, but it also has drawbacks, since the speed of access to the hard disk is much smaller than the speed of access to main memory, so the overall speed of the system will be reduced when the operating system is making use of this file or swap area.

How to check if your system already has a swap area or file

Before we start, let’s take a look to check if we already have an active swapfile. Although we can have multiple swap files or swap partitions, one is usually enough.

To check if you already have an active swapfile, run the following command in the console or terminal of your Linux system:

$ swapon -s

If you do not have root permissions, you must call the previous command, and all those that we will see later, preceded by the sudo command, with which the system will later ask you for the administrator password to execute it.

$ sudo swapon -s

If you don’t have any swapfiles, the response to running the above code is as follows:

Filename Type Size Used Priority

How to check if you have enough space on your hard drive

Before creating our swapfile, let’s make sure we have enough space on our hard drive to create it. To do this, we execute the following command:

$ df -h

which response is as follow:

Filesystem      Size  Used Avail Use% Mounted onudev            236M     0  236M   0% /dev
tmpfs 49M 5.6M 44M 12% /run
/dev/vda1 20G 7.9G 12G 41% /
tmpfs 245M 0 245M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 245M 0 245M 0% /sys/fs/cgroup
tmpfs 49M 0 49M 0% /run/user/0

From what we can see we have several gigabytes still available on our main partition /dev/vda1.

How to create and activate a swapfile in Ubuntu

To create our swapfile, which in this case we are going to make with a size of 2GB, we are going to execute the following command:

$ dd if=/dev/zero of=/swapfile bs=2048 count=1048576

As you can see, the swapfile is in the root path of the system and is called swapfile, and it has 1048576 blocks of 2048 bits, so it has a size of 2GB.

When executing the command, it will return something similar to this:

1048576+0 records in
1048576+0 records out
2147483648 bytes (2.1 GB, 2.0 GiB) copied, 13.6032 s, 158 MB/s

Subsequently, we are going to give a series of permissions to our /swapfile file, thus ensuring that only the root user can access it, avoiding security problems with other users and/or applications. To do this, we execute the following:

$ chmod 600 /swapfile

Next, we are going to indicate to the system that we want to use this file as a swapfile, for this, we execute:

$ mkswap /swapfileSetting up swapspace version 1, size = 2GiB (2147479552 bytes)
no label, UUID=aa071ec1-43c3-4cb5-88d1-f1eb5629f8ff

And finally, we are going to activate our swapfile by executing the command:

$ swapon /swapfile

Ready! If now we execute, as at the beginning, the swapon command, we will see that the file in question is active in our swap area of the Ubuntu system:

$ swapon -sFilename        Type    Size     Used    Priority
/swapfile file 2097148 143536 -1

To finish, we only have to make this swapfile permanent so that it is activated every time the system is restarted. To do this, we access the /etc/fstab file:

$ vi /etc/fstab

and at the end of it we add the following line:

/swapfile none swap sw 0 0

From now on you have the swapfile active in your Linux Ubuntu operating system, so if at any time your system runs out of RAM, it will use part of your hard drive to temporarily store the necessary data.

--

--