How To Add Swap Space on Ubuntu 18.04 / 20.04

5.0
1 review

Introduction

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

Warning: Although swap is generally recommended for systems utilizing traditional spinning hard drives, using swap with SSDs can cause issues with hardware degradation over time. Due to this consideration, we do not recommend enabling swap on DigitalOcean or any other provider that utilizes SSD storage. Doing so can impact the reliability of the underlying hardware for you and your neighbors. This guide is provided as reference for users who may have spinning disk systems elsewhere.

If you need to improve the performance of your server on DigitalOcean, we recommend upgrading your Droplet. This will lead to better results in general and will decrease the likelihood of contributing to hardware issues that can affect your service.

What is Swap?

Swap is an area on a hard drive that has been designated as a place where the operating system can temporarily store data that it can no longer hold in RAM. Basically, this gives you the ability to increase the amount of information that your server can keep in its working “memory”, with some caveats. The swap space on the hard drive will be used mainly when there is no longer sufficient space in RAM to hold in-use application data.

The information written to disk will be significantly slower than information kept in RAM, but the operating system will prefer to keep running application data in memory and use swap for the older data. Overall, having swap space as a fallback for when your system’s RAM is depleted can be a good safety net against out-of-memory exceptions on systems with non-SSD storage available.

Source: DigitalOcean.com

1
Checking the System for Swap Information
1 min
Before we begin, we can check if the system already has some swap space available. It is possible to have multiple swap files or swap partitions, but generally one should be enough.
2
Checking Available Space on the Hard Drive Partition
1 min
Before we create our swap file, we’ll check our current disk usage to make sure we have enough space.
3
Creating a Swap File
1 min
Now that we know our available hard drive space, we can create a swap file on our filesystem. We will allocate a file of the swap size that we want called swapfile in our root (/) directory.
4
Enabling the Swap File
2 min
Now that we have a file of the correct size available, we need to actually turn this into swap space.
5
Making the Swap File Permanent
1 min
Our recent changes have enabled the swap file for the current session. However, if we reboot, the server will not retain the swap settings automatically.
6
Tuning your Swap Settings
2 min
There are a few options that you can configure that will have an impact on your system’s performance when dealing with swap.
5.0
5 out of 5
1 Ratings

Detailed Rating

Stars 5
1
Stars 4
0
Stars 3
0
Stars 2
0
Stars 1
0

{{ review.user }}

{{ review.time }}
 

Show more
Please, login to leave a review
How To Add Swap Space on Ubuntu 18.04 / 20.04
Category:
5 out of 5
5
1 review
Price:
Free