DEV Community

Shawon Saha
Shawon Saha

Posted on • Updated on

SWAP Management - Create, Activate, Swappiness, Deactivate, Remove [EASY GUIDE]

CREATE


  1. Creating a Swap File Calculating Count Value
sudo dd if=/dev/zero of=/swapfile bs=1024 count=<1024*desired_swap-size_in_MB>
Enter fullscreen mode Exit fullscreen mode

Keep block size bs=1024

  1. Set the file permissions to 600 to prevent regular users to write and read the file:
sudo chmod 600 /swapfile
Enter fullscreen mode Exit fullscreen mode
  1. Create a Linux swap area on the file:
sudo mkswap /swapfile
Enter fullscreen mode Exit fullscreen mode

ACTIVATE


  1. Activate swap file
sudo swapon /swapfile
Enter fullscreen mode Exit fullscreen mode
  1. To Make the change permanent open the /etc/fstab file:
sudo nano /etc/fstab
Enter fullscreen mode Exit fullscreen mode
  1. Paste following line
/swapfile swap swap defaults 0 0 
Enter fullscreen mode Exit fullscreen mode

Make Swap permanent


SWAPPINESS


  1. Check swappiness value
cat /proc/sys/vm/swappiness
Enter fullscreen mode Exit fullscreen mode

On Ubuntu, the default swappiness value is set to 60

  1. Modify swappiness
sudo sysctl vm.swappiness=10
Enter fullscreen mode Exit fullscreen mode

While the swappiness value of 60 is OK for most Linux systems, for production servers, you may need to set a lower value.

  1. Make swappiness permanent To make this parameter persistent across reboots, append the following line to the /etc/sysctl.conf file:
vm.swappiness=10
Enter fullscreen mode Exit fullscreen mode

DEACTIVATE


  1. Deactivate swap file
sudo swapoff -v /swapfile
Enter fullscreen mode Exit fullscreen mode

REMOVE


  1. Remove the swap file entry /swapfile swap swap defaults 0 0 from the /etc/fstab file. From Step 5.

  2. Finally, remove the actual swapfile from your disk

sudo rm /swapfile
Enter fullscreen mode Exit fullscreen mode

You can find your swapfile at root directory

swapfile Location

To check the status of swap use free -h or swapon -s

Top comments (0)