Explain how to add swap partition on Ubuntu 16.04

Foreword

One of the easiest ways to improve server response speed and prevent application out of memory errors is to add some swap space. In this guide, we will describe how to add swap files to an Ubuntu 16.04 server.

However, please note:

Although the swap area is generally recommended for systems that use traditional spinning hard drives, using swap with SSDs may cause hardware degradation issues over time. Due to this consideration, we do not recommend enabling swap partitioning on DigitalOcean or any other vendors that use SSD storage. Doing so may affect the reliability of the underlying hardware of you and your neighbors. This guide is only for reference by users who may use spinning disk systems elsewhere. If you need to improve the performance of the DigitalOcean server, we recommend upgrading your Droplet so that you have a better experience and will reduce the possibility of hardware issues affecting your service.

1. First, let's understand what is Swap

Swap partition (also called swap partition) is an area on the hard disk that is designated as a place where the operating system can temporarily store data, and these data can no longer be stored in RAM. Basically, this allows you to increase the amount of information the server retains in the working "memory", but there are some considerations, mainly when there is not enough space in the RAM to accommodate the application data being used, the data on the hard drive will be used Swap space.

Information written to disk will be much slower than information stored in RAM, but the operating system is more willing to store application data in memory and use it to exchange old data. In general, when the system's RAM is exhausted, using swap space as a fallback space may be a good safety net to prevent non-SSD storage systems from running out of memory.

2. Check system exchange information

Before we start, we can check whether the system already has some free swap space, there may be multiple swap files or swap partitions, but it should usually be enough. We can use the following command to check whether the system has a swap partition:

$ sudo swapon --show

If there is no result or no display, it means that the system currently has no swap space available. You can also use the free tool to verify that there is no swap partition currently available.

$ free -h

Output result:

total used free shared buff/cache available
Mem: 488M 36M 104M 652K 348M 426M
Swap: 0B 0B 0B

You can see that the "swap" lines here are all 0, which means that no swap is active on the system.

3. Check the free space on the hard drive partition

The most common way to allocate space for swap is to use a separate partition dedicated to a specific task. However, it is not always feasible to change the partition scheme. We can simply create a swap file that resides on an existing partition.

Before starting, we should check the current disk usage by entering the following command:

$ df -h

Output result:

Filesystem Size Used Avail Use% Mounted on
udev 238M 0 238M 0% /dev
tmpfs 49M 624K 49M 2% /run
/dev/vda1 20G 1.1G 18G 6% /
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/1001

In this case, the device under /dev is our disk. In this example we have enough space (only 1.1G is used), of course, your usage may be different.

Although there are many opinions on the appropriate size of the swap space, it depends on your personal preferences and application requirements. Generally speaking, the equivalent of twice or twice the amount of system memory is a good starting point. Another good experience is that if you just use it as a RAM backup, the size of the swap partition should not exceed 4 GB.

4. Create swap file

Now that we know the available hard disk space, we can create a swap file in the file system. We will create a file named swapfile in our root (/) directory. The best way to create a swap file is to use the fallocate command, which immediately creates a file of pre-allocated size. Since the server RAM size in this example is 512MB, we will create a 1 GB file in this tutorial and adjust it appropriately to meet the needs of your own server:

$ sudo fallocate -l 1G /swapfile

After the creation is complete, we can use this command to verify whether the correct swap space is reserved:

$ ls -lh /swapfile

show result:

$ -rw-r--r--1 root root 1.0G Apr 2511:14/swapfile

This means that our file has created the correct space size.

5. Enable swap file

Now we have a 1 GB file, we need to turn it into swap space

First of all, we need to lock the permissions of the file so that only users with root permissions can read the contents of the file. This prevents ordinary users from accessing the file and avoids major security risks.

Root permissions for locked files:

$ sudo chmod 600/swapfile

Verify permissions:

$ ls -lh /swapfile

show result:

- rw-------1 root root 1.0G Apr 2511:14/swapfile

As you can see, only the root user has enabled the read-write flag.

Next, we can mark the file as swap space with the following command

$ sudo mkswap /swapfile

show result:

Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=6e965805-2ab9-450f-aed6-577e74089dbf

After marking the file, we can enable the swap file and let our system start using it:

$ sudo swapon /swapfile

You can verify whether the swap space is available through the following command:

$ sudo swapon --show

show result:

NAME TYPE SIZE USED PRIO
/swapfile file 1024M 0B -1

At this time, we can check our settings again through free:

$ free -h

show result:

total used free shared buff/cache available
Mem: 488M 37M 96M 652K 354M 425M
Swap: 1.0G 0B 1.0G

You can see that the swap partition has been successfully created with a size of 1.0 G, and the operating system will use it when necessary.

6. Keep swap files permanently

Our recent changes enabled the swap file for the current session, but if we restart, the server will not automatically retain the swap settings, we can change this by adding the swap file to the /etc/fstab file.

Back up the /etc/fstab file to prevent errors:

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

Add the swap file information to the end of the /etc/fstab file:

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

This preserves the swap file.

7. Adjust your exchange settings

When dealing with exchange, you can configure several options, these options will affect the performance of the system

7.1 Adjust the swappiness attribute

The swappiness parameter configures how often your system swaps data from RAM to swap space. The value is between 0 and 100 and represents a percentage. If the swappiness value is close to 0, the kernel will not swap data to disk unless absolutely necessary. One thing to keep in mind is that interaction with swap files is "expensive", because interaction with swap takes longer than interaction with RAM and can cause a significant drop in performance. The system is less dependent on swap partitions and usually makes your system faster. A value of swappiness close to 100 will try to put more data into the swap to keep more RAM space. Depending on the memory profile of your application or the server you use, this may be better in some cases.

View the current swappiness value:

$ cat /proc/sys/vm/swappiness

The results show that

60

For desktop systems, the swappiness setting of 60 is not a bad value, but for servers, you may want to set it to a value closer to 0.

We can use the sysctl command to set swappiness to different values, for example, to set swappiness to 10:

$ sudo sysctl vm.swappiness=10

show result:

vm.swappiness = 10

This setting will remain until the next system restart. If you want to take effect after restarting, we can do so by adding a line to the /etc/sysctl.conf file:

$ sudo nano /etc/sysctl.conf

Add at the end of the file:

vm.swappiness=10

Save and close the file when you are done.

7.2 Adjust cache pressure setting

Another related value you may want to modify is vfs_cache_pressure. This setting configures how much data the system will choose to cache inode and dentry information. Basically, this is to access data about the file system, which is usually a very time-consuming query and frequent request, so this is a good thing to make your system cache, you can check the current value by querying the proc file system again .

$ cat /proc/sys/vm/vfs_cache_pressure

Output result:

100

This configuration may make our system delete inode information from the cache too quickly. We can set a more conservative value, such as 50.

$ sudo sysctl vm.vfs_cache_pressure=50

show result:

vm.vfs_cache_pressure = 50

Similar to swappiness, this is only valid for the current session, we can change it by adding it to our configuration file, just like we use our swappiness setting:

$ sudo nano /etc/sysctl.conf

Add at the end:

vm.vfs_cache_pressure=50

Save and close the file when you are done.

8. to sum up

Following the steps in this guide will give you some breathing space, otherwise it will cause out of memory exceptions. The swap space is very useful to avoid these common problems if you encounter OOM (out of memory) errors, or if you find that the system is not available. Application, the best solution is to optimize the application configuration or upgrade the server.

Translated from: How To Add Swap Space on Ubuntu 16.04

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Explain how to add swap partition on Ubuntu 16.04
How to add swap space on Ubuntu 20.04
How to add swap partition in Ubuntu
How to add swap on CentOS 7
How to add Apt software source on Ubuntu
Explain how to set static IP on ubuntu14.04
How to add logging module to Nginx on Ubuntu 16.04
How to add the gzip module to Nginx on Ubuntu 14.04
How to install Ruby on Ubuntu 20.04
How to install Memcached on Ubuntu 20.04
How to install Java on Ubuntu 20.04
How to install MySQL on Ubuntu 20.04
How to install VirtualBox on Ubuntu 20.04
How to install Elasticsearch on Ubuntu 20.04
How to install Protobuf 3 on Ubuntu
How to install Nginx on Ubuntu 20.04
How to install Apache on Ubuntu 20.04
How to install Git on Ubuntu 20.04
How to install Node.js on Ubuntu 16.04
How to install MySQL on Ubuntu 20.04
How to install Vagrant on Ubuntu 20.04
How to install Bacula-Web on Ubuntu 14.04
How to install PostgreSQL on Ubuntu 16.04
How to install Git on Ubuntu 20.04
How to install Anaconda3 on Ubuntu 18.04
How to install Memcached on Ubuntu 18.04
How to install Jenkins on Ubuntu 16.04
How to install MemSQL on Ubuntu 14.04
How to install Go on Ubuntu 20.04
How to install MongoDB on Ubuntu 16.04
How to install Mailpile on Ubuntu 14.04
How to install PrestaShop on Ubuntu 16.04
How to upgrade to PHP 7 on Ubuntu 14.04
How to install Skype on Ubuntu 20.04
How to install Jenkins on Ubuntu 20.04
How to install Python 3.8 on Ubuntu 18.04
How to install KVM on Ubuntu 20.04
How to install opencv3.0.0 on ubuntu14.04
How to install Anaconda on Ubuntu 20.04
How to install Prometheus on Ubuntu 16.04
How to deploy Django on Ubuntu 14.04
How to install Apache on Ubuntu 20.04
How to install R on Ubuntu 20.04
How to install Moodle on Ubuntu 16.04
How to install Solr 5.2.1 on Ubuntu 14.04
How to install Teamviewer on Ubuntu 16.04
How to secure Nginx on Ubuntu 14.04
How to install MariaDB on Ubuntu 20.04
How to install Nginx on Ubuntu 20.04
How to install Mono on Ubuntu 20.04
How to install Go on Ubuntu 20.04
How to install Zoom on Ubuntu 20.04
How to uninstall software on Ubuntu
How to install Nginx on Ubuntu 16.04
How to install OpenCV on Ubuntu 20.04
How to install Spotify on Ubuntu 20.04
How to install Postman on Ubuntu 18.04
How to install Go 1.6 on Ubuntu 16.04
How to install Go on Ubuntu 18.04
How to install MySQL on Ubuntu 14.04
How to install PostgreSQL on Ubuntu 20.04