SWAP RAM UbUNTU

Paste Content

Check current memory and swap status
sudo swapon --show
This will list you list of swap if available currently on your system.

You can verify no swap memory by running free command and see swap usage, should be 0.

free -h
Check available hard drive storage for swap
To check free hard drive space, you can run following command and see if enough space is available for swap usage.

df -h
Creating swap file
Now since we are aware of available space in our system, you can now choose size of the swap. Usually 1x or 2x size of your RAM is ideal for swap memory.

to create swap first we need to allocate memory and create file for specified size

sudo fallocate -l 2G /swapfile
This will quickly create 2G of file which we will turn into swap.

we can verify correct file size by running command

ls -lh /swapfile
Output

-rw-r--r-- 1 root root 2.0G Mar 11 11:14 /swapfile
Enable swap file
Before enabling swap on this file, we need to lock this file so that no other user than root can access/modify this.

sudo chmod 600 /swapfile
Verify permission using

ls -lh /swapfile
Output

-rw------- 1 root root 1.0G Apr 25 11:14 /swapfile
Now we can mark this file as swap using following

sudo mkswap /swapfile
Output

Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=95040bf9-5847-4c5c-acee-d1258b272e61
After making swap, lets enable swaping on this file.

sudo swapon /swapfile
Verify the same using

sudo swapon --show
output

NAME      TYPE SIZE USED PRIO
/swapfile file   2G   0B   -1
Now check and verify from free command

free -h
Output

total        used        free      shared  buff/cache   available          990M        356M        207M         52M        425M        397M
Swap:          2.0G          0B        2.0G
Make swap permanent
Thats it for the session, but this will not be permanent swap, once we reboot it, this storage will get deleted.

To make swap memory permanent, we need to update /etc/fstab file.

Before updating, first take backup of existing file.

sudo cp /etc/fstab /etc/fstab.bak
Add the swap file information to the end of your /etc/fstab file by typing:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Wow, you have done it.

You can stop here for most of the cases and your swap is ready to be used by your operating system.

Adjusting Swappiness settings
Swappiness settings means the configuration which will allow kernel to decide when to save file to swap. this value varies from 0–100.

Where 0 means kernel will not store anything in swap unless absolute necessary, and 100 means kernel can use it very often.

Based on your requirement, you can configure this value.

To check your current value, type

cat /proc/sys/vm/swappiness
output

60
You can adjust this setting using

sudo sysctl vm.swappiness=10
This setting will stay until next reboot, to make changes permanent, we will save this value to file.

Edit following file.

sudo nano /etc/sysctl.conf
At the bottom of this, add

vm.swappiness=10
Save and close the file.

You are done.

Comments

No comments yet.