Create and remove swap files in Ubuntu LinuxLinux Tutorials
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.
Recommended swap file size based on RAM
|RAM Size||Swap Size (without Hibernation)||Swap Size (with Hibernation)||why?|
|512MB||512MB||512MB||need more memory to hold the data.|
|1GB||1GB||1GB||need more memory to hold the data.|
|2GB||1GB||2GB||using less slower memory to keep the system fast, but make swap file to help OS in case of full RAM.|
|3GB||1GB||2GB||using less slower memory to keep the system fast, but make swap file to help OS in case of full RAM.|
|4GB||1GB||4GB||using less slower memory to keep the system fast, but make swap file to help OS in case of full RAM.|
|5GB||2GB||4GB||using less slower memory to keep the system fast, but make swap file to help OS in case of full RAM.|
|6GB||2GB||4GB||using less slower memory to keep the system fast, but make swap file to help OS in case of full RAM.|
|8GB||1GB||4GB||using as little as possible of swap because the RAM is large enough to hold the data.|
|16GB||1GB||8GB||using as little as possible of swap because the RAM is large enough to hold the data.|
|32GB||—||8GB||No 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
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
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.
activate swap memory, so the system can use it.
use a text editor (such as gedit or nano or VIM) to add this line to
/swapfile swap swap defaults 0 0
verify the new swap file configuration using
free -m or
swapon -s or
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
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.