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

Rajesh Kumar
Follow me