How to add swap space in Alpine Linux

Are you running low on memory on your Alpine Linux system? Swap space can be a great solution for this issue.

How to add swap space in Alpine Linux

Adding Swap Space in Alpine Linux

Alpine Linux is a lightweight Linux distribution that is designed for security, simplicity, and resource efficiency. It is often used in embedded systems, containers, and virtual machines, and it is becoming increasingly popular for web services and micro services. However, due to its minimalistic design, Alpine Linux may not come with a swap space enabled by default. In this case, you may need to add swap space to your Alpine Linux system.

Swap space is a portion of your hard drive that is used as virtual memory by your operating system. When your system runs out of physical memory (RAM), it starts using swap space to store data. This can help to prevent your system from crashing or freezing when it runs out of RAM.


Before You Begin

Swap should not be seen as a replacement to physical memory. Since swap space is a section of the hard drive, it has a slower access time than physical memory. If your system constantly runs out of memory, you should add more RAM.

  • How to calculate the needed swap space in Alpine?

Generally, the size of the swap file depends on how much RAM your system has:

The swap space in Alpine should equal 2x physical RAM for up to 2 GB of physical RAM, and then an additional 1x physical RAM for any amount above 2 GB, but never less than 32 MB. For Alpine systems with really large amounts of RAM (more than 32 GB), you can likely get away with a smaller swap partition (around 1/2 half the physical RAM).

// M = Amount of RAM in GB.
// S = Amount of Swap Memory in GB.

if (M < 2):
   S = M * 2
else if (M > 32):
  S = M / 2
else:
  S = M

So we can say:

  1. A system with 2 GB of physical RAM would have 4 GB of swap memory.
  2. A system with 3 GB of physical RAM would have 3 GB of swap memory.
  3. A system with 32 GB of physical RAM would have 16 GB of swap memory.

Creating a large swap space partition can be especially helpful if you plan to upgrade your RAM at a later time.

  • How to check if my Alpine installation already has swap enabled ?

Before you start, check if your Alpine installation already has swap enabled by typing: cat /proc/swaps

[root@krython ~]# cat /proc/swaps
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       2843600 0       -2

Alternatively, use the swapon --show command:

[root@krython ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       2843600 0       -2

If the outcome is empty it means that your system does not have swap space enabled.


Create a swap file

The user that you are logged in need to be a root user or need to have sudo privileges to be able to include and also create swap space.

We will add 2GB of Swap memory to our Alpine Linux System.

💡
We will use the dd command to create our swap file and the dd command accept the size in KB

1 GB == 1048576 KB

If you want a 2GB of Swap Space: 2 GB = 2 x 1048576 KB = 2097152 KB
If you want a 8GB of Swap Space: 8 GB = 8 × 1048576 KB = 8388608 KB
If you want a 15GB of Swap Space: 15 GB = 15 × 1048576 KB = 15728640 KB

Let's begin:

  1. Create the swap file that will be used as a Swap space:
sudo dd if=/dev/zero of=/swapfile bs=1024 count=2097152
💡
The command you provided will create a swap file with a size of 2 gigabytes.

Let me break down the command for you:

bs=1024: This sets the block size to 1024 bytes.
count=2097152: This sets the number of blocks to 2097152.

Multiplying the block size by the number of blocks gives you the total size of the file:

1024bytes/block×2097152blocks=2147483648bytes
  1. Ensure that just the root user can read and also write the swap file by setting the correct permissions:
sudo chmod 600 /swapfile
  1. Set up the file as Linux swap area:
sudo mkswap /swapfile
  1. Activate the swap space A short-lived turn on (lost on reboot):
sudo swapon /swapfile
  1. Customize the /etc/fstab to immediately add the new swap at system boot time by adding the following line:
/swapfile swap swap defaults 0 0
  1. Confirm that the swap is active by using either the swapon or the free command:
[root@krython ~]# sudo swapon -s
NAME                 TYPE      SIZE USED PRIO
/swapfile partition 2.1G   0B   -2

[root@krython ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:            809         160         258           5         389         521
Swap:          2195           0        2195

What is swappiness, how to change it's value?

Swappiness is a Linux kernel property that sets the balance between swapping out pages from the physical memory to the swap space and removing pages from the page cache. It basically defines how often the system will use the swap space.

  • swappiness can have a value of between 0 as well as 100
  • swappiness=0 tells the kernel to avoid swapping processes out of physical memory for as long as feasible
  • swappiness=100 informs the kernel to boldy swap processes out of physical memory as well as relocate them to swap cache

The default setup in CentOS is swappiness=30.

Lowering the default value of swappiness will possibly boost general performance for a regular CentOS desktop setup.

A value of swappiness=10 is suggested, yet feel free to experiment.

Remember: CentOS server installments have different performance requirements to desktop systems, and likewise the default value of 30 is likely preferable.

  1. Check the swappiness value:
cat /proc/sys/vm/swappiness
  1. Change the swappiness value A temporary change (lost on reboot):
sudo sysctl vm.swappiness=60
  1. Make the change permanent, edit the configuration file located in /etc/sysctl.conf .
  2. Search for vm.swappiness and change its value as desired. If vm.swappiness does not exist, add it to the end of the file like so:
vm.swappiness=60
  1. Finally, reboot your system:
sudo reboot


Remove a swap file

You will do the opposite steps of the setup process, deactivate swap space then remove the swap file

  1. Deactivating the swap space:
sudo swapoff -v /swapfile
  1. Remove the swap file entry from the /etc/fstab file:
/swapfile swap swap defaults 0 0

remove this line

  1. Finally, Delete the swap file:
sudo rm -rf /swapfile


Conclusion

You have learned :

  • How to add swap space to an Alpine Linux system through the creation of a swap file, setting up and enabling the file, and making changes permanent by editing the /etc/fstab file
  • How to check the existing swap space on the system
  • How swappiness controls the system's use of swap space and how it can be changed to optimize system performance and stability
  • The trade-off between performance and stability when adjusting the swappiness value

Enjoying our content? Your support keeps us going! 🚀

Consider buying us a coffee to help fuel our creativity.