On Ubuntu 16.04, swap space can be quite important for maximizing system speed. particularly on systems with low amounts of physical memory. When physical RAM runs out, the system can temporarily store dormant memory pages in swap space, which functions as virtual memory.

This tutorial will walk you through the process of adding swap space to your Ubuntu 16.04 system. It runs more smoothly and doesn’t crash because of memory issues. For the support of data science and machine learning for Python allocation, the anaconda is been used. As mentioned the purpose of anaconda installation is a task you need to understand in detail.

Examine the System for Information on Swaps

We can see if the system has any swap space available before we start. While it is possible to have more than one swap file or swap partition, one should suffice.

Entering this will show us if the system has any swap configured:

$ sudo swapon --show

If no output is returned, swap space is not currently available on your machine.

You can use the following free utility to confirm that there isn’t an ongoing swap:

$ free -h

check system for swap info

The output’s “Swap” row shows you that the system is not currently in use for any swaps.

Examine the Hard Drive Partition’s Available Space.

Using a separate partition just to allocate space for swap is the most popular method. It isn’t always feasible to change the partitioning scheme, though. A swap file that is stored on an already-existing partition can be made just as simply.

We should first type the following to see how much disk space is being used:

$ df -h

check available space on partitoin

In this instance, the device under /dev is our disk. In this scenario, we have plenty of space (only 1.1G used). You’ll most likely use it differently.

The right size for a swap space is a matter of opinion, but ultimately it comes down to your preferences and the specifications of your application. Generally speaking, a decent starting point is to add twice as much RAM as your system has. Anything more than 4G of swap is generally unnecessary if you only use it as a RAM fallback, which is another wise maxim.

Make a Swap File.

We can create a swap file inside our filesystem now that we are aware of the amount of hard disk space that we have available. In our root (/) directory, we will create a file called swapfile with the desired swap size. The fallocate software is the most effective method for producing a swap file. With this command, a file with a preallocated size is created quickly.

As the server in our case has 512MB of RAM, we will use this approach to build a 1 Gigabyte file. Modify this to suit your own server’s requirements:

$ sudo fallocate -l 1G /swapfile

By typing: we can confirm that the appropriate quantity of space was reserved.

$ ls -lh /swapfile

We have allocated the appropriate amount of space when creating our file.

Activating the Swap File

We need to convert this into swap space now that we have a file available in the appropriate size. Initially, we must restrict the file’s permissions such that only users with root access can view its contents. This has serious security consequences because it keeps regular users from accessing the file.

To restrict file access to only root, type:

$ sudo chmod 600 /swapfile

Type this to confirm the permissions change:

$ ls -lh /swapfile

enabling swap file

As you can see, read and write flags are only enabled for the root user.

The file can now be designated as swap space by typing:

$ sudo mkswap /swapfile

mark file as swap

Once the file has been marked, we may activate the swap file so that our system can begin using it:

$ sudo swapon /swapfile

By typing:, we can confirm that the swap is available.

$ sudo swapon --show

verify that swap is available

To confirm our conclusions, we can review the output of the free utility once more:

$ free -h

check the output of free utility

After successfully configuring our swap, our operating system will use it when needed.

Set the swap file as a permanent one.

The swap file is now active for this session thanks to our recent modifications. But the server won’t automatically remember the swap settings if we reboot. By include the swap file in our /etc/fstab file, we can alter this.

The /etc/fstab file should be backed up in case something goes wrong:

$ sudo cp /etc/fstab /etc/fstab.bak

You can type the following to append the swap file information to the end of your /etc/fstab file:

$ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Adjust the Swap Settings

When it comes to swapping, there are a few configuration choices that you may play with to see how your system performs.

Modifying the Swappiness Characteristic

The frequency at which your system moves data from RAM to the swap area is set by the swappiness option. A number between 0 and 100 is used to represent the percentage. The kernel won’t swap data to the disk unless it is required with values around zero. Recall that swap file interactions are “expensive” in the sense that they can significantly impair performance and take a lot longer than RAM interactions. In general, your system will operate faster if you tell it not to rely as much on the swap.

To maintain more RAM-free space, values that are closer to 100 will attempt to swap out more data. This might be preferable in some circumstances, depending on the memory profile of your apps or the purpose of your server.

By typing, we may view the swappiness value as of right now.

$ cat /proc/sys/vm/swappiness

can see current swappiness value

A swappiness setting of 60 is not too bad for a desktop computer. You may want to shift the server closer to 0. Using the sysctl command, we can change the swappiness value.

For example, we may type: to set the swappiness to 10.

$ sudo sysctl vm.swappiness=10

for instance to set the swappiness to 10

This configuration won’t change till the following reboot. We may add the following line to our /etc/sysctl.conf file to set this value automatically at restart:

$ sudo nano /etc/sysctl.conf

At the end, you can include:

at the bottom you can add

Close and save the file when you’re finished.

Changing the Cache Pressure Configuration

It is possible that you would also like to adjust the vfs_cache_pressure value. The amount that the system will prioritize caching inode and dentry information over other data is configured by this parameter.

In essence, this is filesystem access data. It is sometimes very expensive to search and highly requested, so it is a great idea for your system to cache this. You may query the proc filesystem once more to see the current value:

$ cat /proc/sys/vm/vfs_cache_pressure

adjusting the cache pressure setting

Our system deletes inode information from the cache as soon as it is right now. By typing: we can adjust this to a more cautious setting, such as 50.

$ sudo sysctl vm.vfs_cache_pressure=50

output vm vfs cache pressure

Once more, this is exclusive to our present meeting. In the same way that we added our swappiness parameter to our configuration file, we can modify that:

$ sudo nano /etc/sysctl.conf

Add the line with your revised value at the bottom:

add line that value your new value

Close and save the file when you’re finished.

Wrapping Up about How to Add Swap Space on Ubuntu

Finally, swap space addition for Ubuntu 16.04 is a simple procedure that can greatly improve system stability and speed, especially on systems with low physical memory. You can efficiently boost your system’s memory capacity, enabling more seamless multitasking and averting memory-related problems, by following the instructions provided in this guide. For a real-time search and analysis of log data, Elasticsearch has been used and to understand the installation of Elasticsearch on Ubuntu one needs to follow the steps particularly.

If you follow this guide’s instructions, you’ll be able to avoid out-of-memory exceptions in certain situations. Swap space is a very helpful tool for preventing some of these typical issues. Optimizing your application configurations or upgrading your server is the best course of action if you are experiencing OOM (out-of-memory) errors or if your system is not able to run the apps you require.

Frequently Asked Questions (FAQ)

1. What is Swap Space?

It is a virtual memory file used by the operating system. It is used to extend physical RAM. When physical RAM is full, the system stores less frequently used data in swap space to free up RAM for more active processes.

2. Which method is better for Adding Swap Space?

There are two methods for adding swap space as follows:

Swap Files: Swap files are more flexible and easily resized but they have less performance than swap partitions.

Swap Partition: Swap partitions are more performant than swap files but they require proper planning and are not more flexible.

So both have their advantages and disadvantages and you can choose which is better for your requirement.