Create a Swap file on AlmaLinux / Rocky Linux
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.
Why do I need a swap file?
- 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.
- 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.
- 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.
- 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.
Recent Posts
Recent Comments
- anonymous1024 on Pi-hole vs AdGuard Home
- Konrad on Add https to Pi-hole with caddy
- jolu on Add LetsEncrypt SSL certificate to Pi-hole
- Mike on Add https to Pi-hole with caddy
- Kamyar on Pi-hole vs AdGuard Home
6 Comments. Leave new
Thanks for sharing, was the easy way to create a SWAP file on CentOS 7.
The following does not work:
sudo echo “/swap swap swap sw 0 0” >> /etc/fstab
Redirections don’t carry the permission of the command. It has to be like this instead:
sudo bash -c ‘echo “/swap swap swap sw 0 0” >> /etc/fstab’
But thank you for the guide! Helpful 🙂
Thanks for sharing this guide.
thanx,very clean
Thank you!
Thank you!