Main > Servers > CentOS: How to add swap file

CentOS: How to add swap file

14.04.2024 20 comments » Views: 14

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

if 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

Author: | Rating: 4/5 | Tags: , , , , ,

20 comments.

Write a comment
  1. John Reply
    22.04.2024 в 12:33 pm
    How does adding swap affect system performance during regular operations?
    • Vitaliy Orlov Reply
      22.04.2024 в 12:42 pm
      Hi, 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.
  2. Claudia Reply
    19.04.2024 в 6:31 pm
    Is there any risk of data loss or corruption when using swap?
    • Vitaliy Orlov Reply
      22.04.2024 в 12:42 pm
      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.
  3. Alex Reply
    18.04.2024 в 7:15 am
    Can I resize the swap file if I later find that 4GB isn't enough?
    • Vitaliy Orlov Reply
      22.04.2024 в 12:42 pm
      Hello
      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.
      Comment: How does setting the swappiness value affect system behavior?
  4. Elza Li Reply
    17.04.2024 в 10:10 pm
    How does setting the swappiness value affect system behavior?
    • Vitaliy Orlov Reply
      22.04.2024 в 12:43 pm
      Hi Elza, 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.
  5. Henry Oswoll Reply
    17.04.2024 в 2:51 pm
    Will adding swap improve system stability during peak loads?
    • Vitaliy Orlov Reply
      22.04.2024 в 12:44 pm
      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.
  6. Guest Reply
    16.04.2024 в 10:46 am
    What happens if the swap file becomes corrupted?
    • Vitaliy Orlov Reply
      22.04.2024 в 12:44 pm
      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.
  7. Katy F Reply
    16.04.2024 в 2:12 am
    Is there a recommended size for the swap file based on system memory?
    • Vitaliy Orlov Reply
      22.04.2024 в 12:44 pm
      Hello Katy, 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.
  8. Student Reply
    15.04.2024 в 4:33 pm
    Can I use a different file system for the swap file?
    • Vitaliy Orlov Reply
      22.04.2024 в 12:44 pm
      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.
  9. Pro max Reply
    15.04.2024 в 12:40 pm
    Hi there, how do I monitor swap usage?
    • Vitaliy Orlov Reply
      22.04.2024 в 12:45 pm
      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.
  10. Crazy Granny Reply
    14.04.2024 в 7:41 pm
    Cool article bro, do you know are there any alternative solutions to adding swap for managing memory usage?
    • Vitaliy Orlov Reply
      22.04.2024 в 12:46 pm
      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.

Leave a Reply

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

Allowed HTML-tags: <a>, <code>, <i>, <em>, <strong>, <b>, <u>, <strike>