How To Create Swap Memory on Ubuntu


There are two basic sorts of memory during a typical computer. the primary type, random access memory (RAM), is employed to store data and programs while they’re being actively employed by the pc. Programs and data can’t be employed by the pc unless they’re stored in RAM. RAM is volatile memory; that’s, the info stored in RAM is lost if the pc is turned off.

Hard drives are magnetic media used for long-term storage of knowledge and programs. Magnetic media is nonvolatile; the info stored on a disk remains even when power is far away from the pc. The CPU (central processing unit) cannot directly access the programs and data on the hard drive; it must be copied into RAM first, which is where the CPU can access its programming instructions and therefore the data to be operated on by those instructions. During the boot process, a computer copies specific OS programs, like the kernel and init or systemd, and data from the disk drive into RAM, where it’s accessed directly by the computer’s processor, the CPU.

The second sort of memory in modern Linux systems is swap file.

Warning: Although swap is usually suggested for systems using conventional spinning hard drives, placing the swap on SSDs can cause problems with hardware disgrace over time. So we do not recommend enabling the swap on the provider that uses SSD storage.

What is the Swap?

One way to protect against out-of-memory errors in applications is to feature some swap file to your server. In this guide, we will cover the way to add swap space to an Ubuntu server.

The swap may be a portion of disk drive storage that has been put aside for the OS to temporarily store data that it can not hold in RAM. This allows you to increase the quantity of data that your server can confine its memory, with some caveats. The swap file on the disk drive is going to be used mainly when there’s not sufficient space in RAM to carry in-use application data.

The information written to disk is going to be significantly slower than information kept in RAM, but the OS will like better to keep running application data in memory and use swap for the older data. Overall, having swap file as a fallback for when your system’s RAM is depleted are often an honest safety net against out-of-memory exceptions on systems with non-SSD storage available.

Check the System for Swap Information

Before continuing with this tutorial, check if your Ubuntu installation already has swap enabled by typing:

$ sudo swapon --show

If the output is empty, it means your system doesn’t have swap space enabled.

Otherwise, if you get something like below, you have already got swap enabled on your machine.



/dev/sda2 partition 1G 0B -2

Although possible, it’s not common to possess multiple swap spaces on one machine.

Check Available Space

The most common way of allocating space for swap is to use a separate partition dedicated to the task. However, altering the partitioning scheme isn’t always possible. we will even as easily create a swap space that resides on an existing partition.

Before we do that, we should always check the present disk usage by typing:

$ df -h


Filesystem Size Used Avail Use% Mounted on

udev 238M 0 238M 0% /dev

tmpfs 49M 624K 49M 2% /run

/dev/vda1 20G 2.1G 17G 11% /

tmpfs 245M 0 245M 0% /dev/shm

The device under /dev is our disk during this case. we’ve many spaces available during this example (only 2.1G used). Your usage will probably vary.

Although there are many opinions about the acceptable size of the swap file, it really depends on your personal preferences and your application requirements. Generally, an amount adequate to or double the quantity of RAM on your system may be a good start line. Another good rule of thumb is that anything over 4G of swap is perhaps unnecessary if you’re just using it as a RAM fallback.

Creating a Swap Space

The user you’re logged in as must have sudo privileges to be ready to activate swap. during this example, we’ll add 1G swap. If you would like to feature more swap, replace 1G with the dimensions of the swap file you would like.

Perform the steps below to feature swap file on Ubuntu.

Start by creating a file which will be used for swap:

$ sudo fallocate -l 1G /swapfile

If fallocate isn’t installed otherwise you get a mistake message saying fallocate failed: Operation not supported then use the subsequent command to make the swap file:

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

Set the correct permissions by, only the root user should be able to write and read the swap file:

$ sudo chmod 600 /swapfile

Use the mkswap utility to line up a Linux swap area on the file:

$ sudo mkswap /swapfile

Activate the swap space using the following command:

$ sudo swapon /swapfile

To effect the change permanent open the /etc/fstab file:

$ sudo nano /etc/fstab

and paste the next line:


/swapfile swap swap defaults 0 0

Verify created the swap space

We can verify that the swap is available by typing:

$ sudo swapon --show



/swapfile file 1024M 0B -1

We can check the output of the free utility again to corroborate our findings:

$ sudo free -h


              total used free shared buff/cache available

Mem: 488M 37M 96M 652K 354M 425M

Swap: 1.0G 0B 1.0G

Modifying the Swap settings

There are a couple of options that you simply can configure which will have an impression on your system’s performance when handling swap.

Modifying the Swappiness Property

The swappiness parameter configures how often your system swaps data out of RAM to the swap file. this is often worth between 0 and 100 that represents a percentage.

With values on the brink of zero, the kernel won’t swap data to the disk unless necessary. Remember, interactions with the swap space are “expensive” therein they take tons longer than interactions with RAM and that they can cause a big reduction in performance. Telling the system to not believe the swap much will generally make your system faster.

Values that are closer to 100 will attempt to put more data into swap in an attempt to stay more RAM space free. counting on your applications’ memory profile or what you’re using your server for, this could be better in some cases.

The default swappiness value is 60. you’ll check the present swappiness value by typing the subsequent command:

$ cat /proc/sys/vm/swappiness



For a Desktop, a swappiness setting of 60 isn’t a nasty value. For a server, you would possibly want to maneuver it closer to 0.

We can set the swappiness to a special value by using the sysctl command.

For instance, to line the swappiness to 10, we could type:

$ sudo sysctl vm.swappiness=10


vm.swappiness = 10

To make this parameter persistent across reboots, append the subsequent line to the /etc/sysctl.conf file:

$ sudo nano /etc/sysctl.conf

At the bottom, you can add:



The optimal swappiness value depends on your system workload and the way the memory is getting used . you ought to adjust this parameter in small increments to seek out an optimal value.

Adjusting the Cache Pressure Setting

Another related value that you simply might want to switch is vfs_cache_pressure. This setting configures what proportion the system will prefer to cache inode and dentry information over other data.

This is often access data about the filesystem. this is often generally very costly to seem up and frequently requested, so it’s a superb thing for your system to cache. you’ll see the present value by querying the proc filesystem again:

$ cat /proc/sys/vm/vfs_cache_pressure



As it is currently configured, our system removes inode information from the cache too quickly. we will set this to a more conservative setting like 50 by typing:

$ sudo sysctl vm.vfs_cache_pressure=50


vm.vfs_cache_pressure = 50

Again, this is often only valid for our current session. we will change that by adding it to our configuration file as we did with our swappiness setting:

$ sudo nano /etc/sysctl.conf

In the end, add the line that specifies your new value:



Save and close the file when you are done.

Removing a Swap Space

Begin by removing the swap file by typing:

$ sudo swapoff -v /swapfile

Next, remove the swap space entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.

Finally, remove the particular swapfile file using the rm command:

$ sudo rm /swapfile