How to Initialize Virtual Memory Using mkswap in Linux

When a massive Linux server is executing complex database queries or high-frequency rendering tasks, the physical RAM modules will frequently hit maximum capacity. If the system mathematically runs out of physical memory, the kernel’s Out-Of-Memory (OOM) killer will violently terminate active processes to prevent a catastrophic crash. To prevent this, you must architect a “Swap” area—a dedicated sector on the physical hard drive that the kernel can mathematically use as virtual RAM. To initialize this highly structured architecture, you must use the mkswap command.

Understanding the Swap Architecture

The mkswap command is a deeply integrated initialization engine. Unlike mkfs, which builds standard filesystems (like ext4) for storing files, mkswap builds a highly specialized, raw magnetic matrix specifically designed for the kernel’s memory paging algorithm. It obliterates any existing data on the target partition and prepares it for virtual memory offloading.

CRITICAL INFRASTRUCTURE WARNING: The mkswap command is mathematically destructive. If you accidentally execute it against your root partition (e.g., /dev/sda1), you will instantly destroy the entire operating system. You must deploy absolute root privileges to execute this command.

Executing the Swap Initialization

Before executing the command, you must mathematically identify an empty, unformatted partition using lsblk or fdisk -l (e.g., /dev/sdb2).

To initialize the swap architecture on that specific partition, open your terminal and type:

sudo mkswap /dev/sdb2

The exact millisecond you press Enter, the engine violently scrubs the partition. It writes a unique UUID and a specialized swap signature header to the raw disk sectors. The partition is now mathematically ready to act as virtual RAM.

Activating the Virtual Memory

While mkswap builds the architecture, it does not turn it on. The kernel will ignore the partition until you explicitly arm it.

To instantly inject the new swap space into the active kernel memory pool, you must execute:

sudo swapon /dev/sdb2

You can verify the successful integration by running the free -m command, which will now mathematically reflect the massive increase in available total swap memory.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.