Enabling a Swapfile on Small Instances

In this article I will explain how to enable a swapfile on small instances, and why it might be useful, even if you do have enough physical memory.

639 views
d

By. Jacob

Edited: 2023-09-10 23:05

sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress
sudo mkswap /swapfile
sudo swapon /swapfile

Note. Here are some nice swap file sizes (repetitions of bs in this case):

  1. 1024
  2. 2048
  3. 4096
  4. 8192
  5. 16384

It may sometimes be preferred to at least have the same amount in swap as in RAM, but it always depends on your needs, including the high memory use scenarios you may run into, and their causes. You can experiment to find out what works best in your case. Some might even have double the amount of swap compared to RAM.

Enabling virtual memory in the form of a swapfile can be useful when trying to avoid running out of memory doing rare peaks in memory usage on a system—even when the system already has enough physical memory. This is because, when a Linux system runs out of memory, it might completely freeze up and/or shut down important processes to free memory.

This might result in /var/log/syslog containing messages like:

... Out of memory: Killed process ...

Enabling a swap file on a Linux system can be done by first using the dd command to format a swapfile, then mkswap and swapon to make and enable the swap. An easy way to format the swapfile is by using the "count" option with "bs"; bs is the blocksize, while count specifies the number of times the blocksize should be repeated.

Why we may want to create a swap file

Website owners will often not make enough money on ads to cover their hosting expenses, and therefor we might want to use the smallest possible virtual servers to host our websites—often this is just a typical LAMP stack (Linux + Apache + MySQL + PHP).

With cloud providers such as AWS or Azure, it is possible to deploy servers of a variety of different sizes. The smallest typically has no more than 512-1024 Megabytes of RAM, and often times this is not enough to secure optimal operation.

Enabling a swap file and/or changing the configuration of different services to use less memory may still allow us to use these smaller instances.

OOM Error while installing MySQL

When trying to install MySQL you might get an error preventing you from proceeding. E.g:

mysql.service: A process of this unit has been killed by the OOM killer.
mysql.service: Main process exited, code=killed, status=9/KILL
mysql.service: Failed with result 'oom-kill'.
Failed to start MySQL Community Server.

When this happens doing the installation, it can be an indication you are trying to install MySQL on a server that does not have enough physical memory. There are various things you can do to still install MySQL; enabling a swap file is one of them.

I have personally run MySQL + PHP + Apache2 on a server with only 512 MB of RAM, so I know it can be done.

Another way to significantly reduce MySQL's memory use is to disable performance schema; to disable it, edit either:

  1. /etc/mysql/mysql.conf.d/mysqld.cnf (preferred for custom settings)
  2. /etc/mysql/my.cnf
  3. /etc/my.cnf
[mysqld]
performance_schema = 0

Enabling swap might increase costs

When using a cloud hosting provider, such as AWS and Azure, enabling a swap file might result in increased costs due to extra IOPS (Input/Output Operations Per Second), but this might depend on storage type. For certain storage types, there should be no additional charges for read/write operations, but performance might slow down once you reach a certain limit.

Note. You should examine the documentation of your hosting provider and storage type to learn the exact details that applies in your case.

As a step towards keeping costs down we can apply certain modifications to our server configuration to keep memory use below the available physical memory. For example, there are settings that can be changed for both MySQL and PHP to lower memory consumption.

Tell us what you think:

  1. Understanding file permissions in Unix / Linux based systems, and how to make files immutable.
  2. How to determine an optimal value for pm.max_children and related php-fpm settings for your server and web applications.
  3. Tutorial showing how to configure a VirtualBox Guest VM with HOST-only and NAT adapter, while using the WWW folder from the HOST OS.
  4. You may have wondered what the /etc/php/8.0/conf.d/ directory is for in Debian and Ubuntu, and whether it is better to edit the conf.d files than editing php.ini directly; find out in this Tutorial.

More in: Linux servers