
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
1 2 3 4 5 |
$ sudo swapon --show NAME TYPE SIZE USED PRIO /swapfile file 4G 0B -2 |
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)
1 2 3 4 5 6 7 |
$ sudo dd if=/dev/zero of=/swapfile bs=4G count=4 dd: memory exhausted by input buffer of size 4294967296 bytes (4,0 GiB) $ sudo ls -alh /swapfile -rw-r--r-- 1 root root 0 14 apr 10.54 /swapfile |
so, I created the file as
1 2 3 4 5 6 |
$ sudo fallocate -l 4G /swapfile $ sudo ls -alh /swapfile -rw-r--r-- 1 root root 4,0G 14 apr 10.56 /swapfile |
then you need to change the permissions for the file, so the only root can access it
1 2 3 |
$ sudo chmod 600 /swapfile |
with the next step you need to format the swap file
1 2 3 4 5 |
$ sudo mkswap /swapfile Setting up swapspace version 1, size = 4 GiB (4294963200 bytes) no label, UUID=61c64757-e97e-4666-a841-6214a93f5eb0 |
and then you should activate it
1 2 3 4 5 6 7 |
$ sudo swapon /swapfile $ sudo swapon --show NAME TYPE SIZE USED PRIO /swapfile file 4G 0B -2 |
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
1 2 3 4 5 6 |
$ sudo nano /etc/fstab ... /swapfile none swap sw 0 0 |
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 )
1 2 3 4 5 |
$ sudo nano /etc/sysctl.conf ... vm.swappiness = 10 |
and apply the changes
1 2 3 |
$ sudo sysctl -p |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php function tryMem($mbyte){ $bytes = 1048576; echo("\tAllocate Attempt {$mbyte} MBs\n"); $dummy = str_repeat("0", $bytes*$mbyte); echo("Current Memory Use: " . memory_get_usage(true)/$bytes . ' MBs'); echo("\tPeak Memory Use: " . memory_get_peak_usage(true)/$bytes . ' MBs'); echo("\n"); } for($i=10;$i<6000;$i+=50){ $limit = $i.'M'; ini_set('memory_limit', $limit); echo("Memory limit set to: ". ini_get("memory_limit")); tryMem($i-10); } |
you can put this code into a script. like allocate-ram.php and execute it as follow
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$ php -d memory_limit=4G allocate-ram.php Memory limit set to: 10M Allocate Attempt 0 MBs Current Memory Use: 2 MBs Peak Memory Use: 2 MBs Memory limit set to: 60M Allocate Attempt 50 MBs Current Memory Use: 2 MBs Peak Memory Use: 52.00390625 MBs Memory limit set to: 110M Allocate Attempt 100 MBs Current Memory Use: 2 MBs Peak Memory Use: 102.00390625 MBs Memory limit set to: 160M Allocate Attempt 150 MBs Current Memory Use: 2 MBs Peak Memory Use: 152.00390625 MBs Memory limit set to: 210M Allocate Attempt 200 MBs Current Memory Use: 2 MBs Peak Memory Use: 202.00390625 MBs .... |
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.