Swap File Maintenance

A SWAP file allows the OS to use hard disk space to simulate additional RAM. When the system lacks memory, it moves some of the memory that the non-running processes uses on the hard disk to free up RAM for other processes. This combination of RAM and SWAP file is known as virtual memory. Using virtual memory allows your computer to run more processes than it could run in RAM alone.

Create SWAP File

To create 1 GB SWAP file use the following commands:

sudo swapon --show

sudo dd if=/dev/zero of=/var/swapfile bs=1M count=1024
sudo chmod 600 /var/swapfile
sudo mkswap /var/swapfile
sudo swapon /var/swapfile

If you prefer different size, you can use count=2048, count=4096, etc.

The swap file will exists till the reboot of the machine. You can make the swap file permanent by adding it to the fstab file.

sudo vim /etc/fstab
/var/swapfile   swap    swap    defaults        0   0

Now just need to turn on the SWAP and check the status

sudo swapon -a
sudo swapon --show

Remove SWAP File

sudo swapoff /var/swapfile
sudo rm /var/swapfile

remove the /var/swapfile line from /etc/fstab
   RAM        SWAP 
256 MB      256 MB
512 MB	    512 MB
  1 GB	      1 GB
  2 GB	      1 GB
  3 GB	      2 GB
  4 GB	      2 GB
  6 GB	      2 GB
  8 GB	      3 GB
 12 GB	      3 GB
 16 GB	      4 GB
 24 GB	      5 GB
 32 GB	      6 GB
 64 GB	      8 GB
128 GB	     11 GB

Contents