Create a Swap file on AlmaLinux / Rocky Linux

Tech
6 Comments

Even if you have sufficient memory in your server, it’s still a good idea to have a swap file – I’ve outlined some of the reasons after the instructions.  In order to reduce disk IO, many cloud providers don’t include one as standard – so you may need to add your own.  Here we go through the steps to create a swap file in AlmaLinux (8 or 9) and Rocky Linux (8 or 9).

Firstly, you can use the following command to check if you have any swap space already:

free -m

The following output shows zero swap space

              total        used        free      shared  buff/cache   available
Mem:           7822        1390         506         492        5924        5569
Swap:             0           0          0

Now we need to create the swap file – I’m going to create a 4GB file, but you can adjust the values to your needs:

sudo dd if=/dev/zero of=/swap count=4096 bs=1MiB

Now set the permissions:

sudo chmod 600 /swap

and format the swap file:

sudo mkswap /swap

To enable the swap file just use:

sudo swapon /swap

To make the swap be automatically mounted after reboot, the following command will add it to your fstab config:

sudo echo "/swap swap swap sw 0 0" >> /etc/fstab

We can now test our swap file to make sure all is well.  Firstly, running ‘free -m’ again should yield a different result:

              total        used        free      shared  buff/cache   available
Mem:           7822        1390         506         492        5924        5569
Swap:          4095           0        4095

And we can check the swap file by using ‘sudo swapon -s’

Filename  Type  Size     Used  Priority
/swap     file  4194300  0     -1

You’re now all setup with your swap file.

Man upgrading memory in computer instead of creating a swap file.

Why do I need a swap file?

  1. Memory Overcommitment: Servers often run multiple processes simultaneously, and there may be situations where the demand for memory exceeds the physical RAM available. In such cases, the operating system can use swap space as virtual memory to temporarily store data that doesn’t fit into physical memory.
  2. Preventing Out of Memory (OOM) Errors: Without swap space, if a server exhausts its physical memory, it may lead to Out of Memory errors, causing applications to crash or the system to become unresponsive. Swap space provides a safety net by allowing the system to move less frequently accessed data from RAM to disk, freeing up physical memory for more critical tasks.
  3. Optimizing Memory Usage: Swap space allows the operating system to optimise memory usage by swapping out less frequently used data and bringing in more frequently accessed data into physical memory. This can help improve overall system performance by ensuring that the most critical processes have access to fast RAM.
  4. Handling Memory Peaks: Some workloads may experience sudden spikes in memory usage. Swap space provides additional memory resources to handle these peaks without affecting system stability or performance.

While swap space can be beneficial, it’s essential to configure it appropriately to avoid performance degradation due to excessive swapping. Over-reliance on swap space can lead to a phenomenon known as “thrashing,” where the system spends more time moving data between RAM and disk than actually executing tasks, resulting in poor performance. Therefore, it’s crucial to monitor swap usage and adjust its size accordingly based on the specific requirements of the server workload.

6 Comments. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu