Create Swap File on Cloud Linux Server to Prevent Out of Memory

When a Linux server runs out of memory, some programs such as MariaDB/MySQL will shutdown automatically. To prevent out of memory (OOM) problem, we can create a swap partition or swap file so as to expand the memory. In this tutorial, we’re going to look at how to create a swap file on Linux server with small memory.

First, let me explain some background information.

Swap Space

When you are installing Linux on a desktop computer or a server, one of the considerations is how much swap space it’s going to use. Swap space is a kind of virtual memory. Linux divides RAM into pages. When the physical RAM starts to fill up, Linux can swap out some pages in the RAM to the swap space on disk. To calculate how much virtual memory your system has, simple add physical RAM and swap space.

Swap space in Linux can be a swap partition, swap file or a combination of them. In windows, it’s just a page file stored in C drive. Normally Linux installers such as Ubuntu Ubiquity and CentOS Anaconda try to set aside a swap partition when installing the system.

To check your Linux system’s swap space, use the swapon --show command. You may need to use sudo.

swapon --show

swapon command

We can get the following information.

  • how many swap partitions or swap files are in our Linux system
  • the size of each swap device
  • how much swap space is being used
  • the priority of each swap device

Priority controls which swap device is used first. Swap devices with a higher number are used before swap devices with lower number.

Create a Swap File

On a cloud Linux server, you may have only one partition for the root file system. In this case, you have no way of creating another partition and format it as swap partition.  In stead we can create a swap file in the root file system.

First, we use fallocate command to create a file. For example, create a file named swapfile with 512M capacity in root file system:

sudo fallocate -l 512M /swapfile

To create a 1G file:

sudo fallocate -l 1G /swapfile

Then make sure only root can read and write to it.

sudo chmod 600 /swapfile

Format it to swap:

sudo mkswap /swapfile

Output:

Setting up swapspace version 1, size = 524284 KiB
no label, UUID=h32b3e10-0779-4865-9ea0-6e2af8f3kea9

Enable the swap file

sudo swapon /swapfile

Now you can see that it’s enabled with swapon --show command.

admin@server:~$ sudo swapon --show
NAME      TYPE  SIZE  USED  PRIO
/swapfile file  512M  132K  -1

To make Linux automatically mount this swap file when booting up, add this line to /etc/fstab file.

/swapfile  none  swap  defaults  0  0

swap file

Please note that you need to delimit each column with Tab key.

How to Disable Swap File on Debian & Ubuntu

Swap space can make your server slower and it can wear out the SSD on your server much faster than you think. If you need better performance, it’s a good idea to upgrade the physical RAM, then disable swap space. To disable swap file, install the dphys-swapfile package.

sudo apt install dphys-swapfile

Then edit the configuration file.

sudo nano /etc/dphys-swapfile

Find the following line.

#CONF_SWAPFILE=/var/swap

By default, the swap file is set to /var/swap. Remove the # character to uncomment this line and change its value to /swapfile.

CONF_SWAPFILE=/swapfile

Next, run the following command, which will fetch all content in the swap file back into the physical RAM, then disable the swap file.

sudo dphys-swapfile swapoff

Now you can delete the swap file to reclaim disk space.

sudo dphys-swapfile uninstall

It’s recommended to disable the dphys-swapfile service, so it won’t automatically create a swap file at boot time.

sudo systemctl disable dphys-swapfile

Wrapping Up

I hope this tutorial helped you use swap space on Linux server. You may also want to read:

As always, if you found this post useful, then subscribe to our free newsletter to get new tutorials 🙂

Rate this tutorial
[Total: 1 Average: 5]

One Response to “Create Swap File on Cloud Linux Server to Prevent Out of Memory

  • Thanks for the tutorial Xiao. Very helpful with out of memory exceptions when installing MySQL with only 512 MB of ram!

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community (https://community.linuxbabe.com) for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image.