🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Linux Tutorials: What is Swap in Linux and Useful Commands

What is Swap in Linux?
Swap is a space on a disk that is used when the amount of physical RAM memory is full. When a Linux system runs out of RAM, inactive pages are moved from the RAM to the swap space.

Swap space can take the form of either a dedicated swap partition or a swap file. In most cases, when running Linux on a virtual machine, a swap partition is not present, so the only option is to create a swap file.

Lets deep dive and understand in more depth.

There are two basic types of memory in a typical computer.

  1. Random access memory (RAM)
    The first type, random access memory (RAM), is used to store data and programs while they are being actively used by the computer. Programs and data cannot be used by the computer unless they are stored in RAM. RAM is volatile memory; that is, the data stored in RAM is lost if the computer is turned off. Hard drives are magnetic media used for long-term storage of data and programs. Magnetic media is nonvolatile; the data stored on a disk remains even when power is removed from the computer. The CPU (central processing unit) cannot directly access the programs and data on the hard drive; it must be copied into RAM first, and that is where the CPU can access its programming instructions and the data to be operated on by those instructions. During the boot process, a computer copies specific operating system programs, such as the kernel and init or systemd, and data from the hard drive into RAM, where it is accessed directly by the computer’s processor, the CPU.

  • Swap space
    The second type of memory in modern Linux systems is swap space. The primary function of swap space is to substitute disk space for RAM memory when real RAM fills up and more space is needed.

Useful Commands to Monitor Swap Space Usage in Linux

How to add Swap File? Create a file that will be used for swap:
$ sudo fallocate -l 1G /swapfile
Only the root user should be able to write and read the swap file. To set the correct permissions type:
$ sudo chmod 600 /swapfile
Use the mkswap utility to set up the file as Linux swap area:
$ sudo mkswap /swapfile
To verify that the swap is active, use either the swapon or the free command as shown below:
$ sudo swapon --show
First, deactivate the swap by typing:
$ sudo swapoff -v /swapfile
Turn off the swap partition with the command which turns off all swap space:
$ swapoff -a
Now display the existing partitions on the hard drive.
$ $ fdisk -l
Remove the swap file entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.
Finally, delete the actual swapfile file using the rm command:
$ sudo rm /swapfile
To view all devices marked as swap in the /etc/fstab file you can use the --all option. Though devices that are already working as swap space are skipped.
$ swapon --all
Disable swap
$ sudo swapoff -a
$ sudo sed -i '/ swap / s/^/#/' /etc/fstab
If you want to view a summary of swap space usage by device, use the --summary option as follows.
$ swapon --summary
The free command is used to display the amount of free and used system memory. Using the free command with -h option, which displays output in a human readable format.
$ free -h

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.