CentOS: How to add swap file
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 |
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)
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
Author: | Tags: /
| Rating:
20 comments.
Write a comment