CentOS: How to add swap file

Swap Memory

I faced the problem on one of my VPS that leaded to the situation when the system kills ClamAv daemon that allocated all the memory during update. The server has only 4Gb of RAM and I didn’t want to increase it as this will increase the payment for this server. Keeping in mind that except of this problem with ClamAv everything works fine I decided to fix this by adding the swap so the system can use it during ClamAv update.

How to check, create and activate the swap.

First of all we need to understand do we have some swap or not, you can check it via htop or via the next command

f the swap extist you will see output like above, if not you will see empty response

In my case I observed that I didn’t have any swap, so I tried to create the file via dd, but recieved an error (because I didn’t have enough free memory)

so, I created the file as

then you need to change the permissions for the file, so the only root can access it

with the next step you need to format the swap file

and then you should activate it

Now you can see that the swap created and can be used. But before this, let’s make this configuration permanent. You need to open your fstab file and append related record

Now swap file will be automatically mounted after reboot.

Actually that’s all, but it also make sense to set priority for the items that will be moved to swap (more info https://phoenixnap.com/kb/swappiness )

and apply the changes

Now everything is ready. But in my case I also rebooted the system, to be sure that everything will work after reboot.

How to test swap

To check the swap we need some process that will increase the memory usage.

I used for this next PHP script

you can put this code into a script. like allocate-ram.php and execute it as follow

during the script execution it increases memory usage by adding 50MB on each next step, so you can see how your memory usage grow and how the system using swap.

To interrupt the script just hit Ctrl+C

FAQ

How does adding swap affect system performance during regular operations?

  • Adding swap generally helps prevent system crashes due to running out of memory, but it can slightly degrade performance during regular operations as the system may need to swap data between RAM and disk.

Is there any risk of data loss or corruption when using swap?

  • Generally, there’s no risk of data loss or corruption when using swap. However, if your system frequently runs out of memory and relies heavily on swap, it can lead to slower performance and increased wear on your storage device.

Can I resize the swap file if I later find that 4GB isn’t enough?

  • Yes, you can resize the swap file by creating a new larger file, copying the contents of the old file into it, and then updating the swap configuration to use the new file.

How does setting the swappiness value affect system behavior?

  • The swappiness value determines how aggressively the kernel swaps out memory pages to disk. A lower value means the system will try to avoid swapping as much as possible, while a higher value means it will swap more aggressively.

Will adding swap improve system stability during peak loads?

  • Yes, adding swap can improve system stability during peak loads by providing extra memory space when physical RAM is exhausted. However, excessive swapping can also degrade performance, so it’s important to monitor and adjust swap usage accordingly.

What happens if the swap file becomes corrupted?

  • If the swap file becomes corrupted, the system may experience instability or crashes. In such cases, it’s recommended to recreate the swap file and activate it again.

Is there a recommended size for the swap file based on system memory?

  • The recommended size for the swap file depends on various factors such as the amount of physical RAM, the workload of the system, and the specific requirements of the applications running on it. As a general rule of thumb, the swap space should be at least equal to the amount of physical RAM, but it can be larger if needed.

Can I use a different file system for the swap file?

  • Yes, you can use a different file system for the swap file, but it’s recommended to use a fast and reliable file system like ext4 or XFS for optimal performance.

How do I monitor swap usage?

  • You can monitor swap usage over time using tools like sar, vmstat, top or htop. These tools provide information about memory and swap usage, allowing you to track trends and identify potential issues.

Do you know are there any alternative solutions to adding swap for managing memory usage?

  • Yes, alternative solutions to adding swap include optimizing memory usage by adjusting application settings (eg limit memory for the docker container) or upgrading physical RAM. It’s important to assess your specific needs and constraints before deciding on the best approach.

You May Also Like

About the Author: vo