Create and remove swap files in Ubuntu Linux

· 631 words · 3 minute read

Computers use RAM to store data and run programs and services. So, the data / programs in RAM are temporarily available — as you use the program or data. When you shutdown your computer, RAM will be free.

RAM is faster than HDD or SSD in read and write speed. So we use it to hold the currently running programs and data. But they are expensive, so we use less of them and rely on swap file to delegate some less used data / program to it.

Swap file is a file stored on your HDD or preferably SSD, to hold the data that the RAM can’t hold because RAM is full. But bear in mind the the Hard Disk Drive or Solid State Drive is still slower than RAM.

So we create a swap file (or swap partition) that helps the operating system run in case of the RAM is full. But we prefer using RAM all the time. So if you have enough RAM to hold all currently used programs, DO NOT CREATE SWAP FILE.

RAM SizeSwap Size (without Hibernation)Swap Size (with Hibernation)why?
512MB512MB512MBneed more memory to hold the data.
1GB1GB1GBneed more memory to hold the data.
2GB1GB2GBusing less slower memory to keep the system fast, but make swap file to help OS in case of full RAM.
3GB1GB2GBusing less slower memory to keep the system fast, but make swap file to help OS in case of full RAM.
4GB1GB4GBusing less slower memory to keep the system fast, but make swap file to help OS in case of full RAM.
5GB2GB4GBusing less slower memory to keep the system fast, but make swap file to help OS in case of full RAM.
6GB2GB4GBusing less slower memory to keep the system fast, but make swap file to help OS in case of full RAM.
8GB1GB4GBusing as little as possible of swap because the RAM is large enough to hold the data.
16GB1GB8GBusing as little as possible of swap because the RAM is large enough to hold the data.
32GB8GBNo need to use swap because RAM is big enough to hold all the data of OS and programs.

How to create a swap file in Linux 🔗

know the current swap file size by this command free -m or using this command swapon -s or even this command swapon -show.

Note: if the output is 000 , the swap file is not configured correctly.

To create the swap file, use this command.

dd if=/dev/zero of=/swapfile bs=1024 count=1048576

where:

if = input file

of = output file

bs = block size. keep it at 1024.

count = amount of blocks to read and write, which helps determine how much space you need for the swap file.

Let the root user ONLY to read and write data to swap file by this command.

chmod 600 /swapfile

set up the swap area by this command.

mkswap /swapfile

activate swap memory, so the system can use it.

swapon /swapfile

use a text editor (such as gedit or nano or VIM) to add this line to /etc/fstab file.

/swapfile swap swap defaults 0 0

verify the new swap file configuration using free -m or swapon -s or swapon --show.

How to remove the swap file in Linux 🔗

use this command to deactivate swap file.

swapoff -v /swapfile

use any text editor to remove swap record / entry from /etc/fstab file.

then remove the actual swap file using this command.

rm -f /swapfile

if it doesn’t removed, use sudo to delete it as a root user.

Share:
waffarx cash back