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

February 28, 2020

When a Linux server is 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 ubiquitous 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 let 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.

Comments, questions or suggestions are always welcome. If you think this post is useful, ? please share it with your friends on social media! Stay tuned for more Linux tutorials.

Need help?

Do you need help setting up this on your own service?
Please contact us and we’ll provide you the best possible quote!