Creating a swap file

What is Swap Space 

The primary function of swap space is to substitute disk space for RAM memory. It allows a computer to use disk space as a temporary storage solution for memory overflow. Swap space provides a way to increase the total amount of available memory, enabling programs to run more effectively.

When a computer's RAM reaches its maximum capacity, the system may start to run out of memory, leading to sluggish performance and system crashes. Swap space acts as a safety net, allowing the operating system to temporarily store unused data and instructions in a swapfile. This file is located on the hard drive, freeing up space in RAM for active processes.

When memory-intensive tasks are executed, the system may transfer unused memory pages to the swap space. This transfer process is known as swapping. It allows the system to free up memory for active processes and ensures that essential data remains accessible when needed.

The swap space should be configured properly to ensure optimal performance. The size and location of the swap file should be carefully determined based on the requirements of the system. Too small a swap space can result in frequent swapping, affecting system performance. Conversely, too large a swap space can lead to unnecessary consumption of disk space.

Swap space is particularly useful for systems with small amounts of RAM, as it enables them to utilize more of the available memory for running applications. Additionally, swap space can be useful in virtual machines, where memory is shared among multiple instances.

Creating Swap Space on a Linux System

Creating a swap space on a Linux system is essential for improving system performance and ensuring sufficient memory resources. 

Swap space plays a crucial role in a Linux system by providing additional memory storage when the physical RAM is running low. In other words, Swap file is necessary for storing information in cases when the amount of RAM is insufficient. Such memory works much slower than RAM, therefore active use of such memory is highly undesirable. It serves as a temporary storage location for memory pages or blocks that are used less frequently, effectively freeing up space in the RAM for more frequently accessed data.

This article provides a step-by-step guide on how to create a swap file on a Linux machine.

Step-by-step guide to creating a swap file

To create swap space on a Linux system, follow these steps:

The first step is to create a swap file. In this case, we create a 2 GB file.

fallocate --length 2GiB /swapfile

Set correct file permissions

chmod 600 /swapfile

We create a swap from the specified file, at the output we get the following.

# mkswap /swapfile
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=97f81d12-80ca-4a61-ad17-1c094f52e36d

Next, enable the use of swap in this file.

# swapon /swapfile

You can check if swap is working with the free command

# free -h
total used free shared buff/cache available
Mem: 3,8G 96M 3,5G 8,5M 268M 3,5G
Swap: 2,0G 0B 2,0G

As you can see in the screenshot, swap is enabled.

To mount this file as a swap file at system startup, you must make the following entry in the / etc / fstab file

/swapfile swap swap defaults 0 0

Instead of the path to the file, you can specify its UUID, obtained as a result of the mkswap command. In this case, the entry will look like this:

UUID=97f81d12-80ca-4a61-ad17-1c094f52e36d swap swap defaults 0 0


Creating a swap file on a Linux system is an essential step to ensure sufficient memory resources and improve system performance. By following the step-by-step guide provided in this document, you can easily create a swap file that meets your requirements.