Getting Started With Swap Files on Linux

This guide will introduce you to swap files in Linux by defining what a swap file is and the important role it plays in keeping your system stable.

What Is a Swap File?

A swap file in Linux is a file on your hard disk that the Linux kernel uses to store variable data of the applications running on your computer. A process known as swapping is responsible for transferring the data to a swap file when the Random Access Memory (RAM) doesn’t suffice.

Imagine you are running a memory-intensive application and the system starts using 100% of the RAM, the Linux operating system will use a swap file to avoid your RAM from filling up and rendering your system non-responsive or worse: crashing.

Note: Reading data from a swap file is a bit slower than reading directly from the RAM but it is a better alternative than having your system crash.

View Your Swap File Using swapon

To get information about the swap file on your system, run the following command:


The output of the command above would be similar to the one shown below.

The output displays that the /root folder contains your system’s swap file named swapfile. In addition, the size of the swap file is 2GB and currently, the system is using around 41 megabytes of the swap file space.

As the RAM and swap space work together to store temporary data, you might want detailed information about both of them. You can do so using the free command.

free -h

How to Create a Swap File on Linux

If you are experiencing performance issues with your RAM then you should consider creating or increasing the size of your swap file.

Related: How to Manage Your Memory on Linux

If you have a system with low RAM, for example, 2GB, you should create a swap file that is about twice the size of your RAM i.e. 4GB.

On the other hand, if you have enough RAM space on your system, for example, 8GB then your swap file can be 25% of your RAM, i.e. 2GB because your RAM can accommodate more application variable data.

Before you can create a new swap file, make sure to disable your current /swapfile:

sudo swapoff /swapfile

Now create a swap file of the size 2GB using the fallocate command.

sudo fallocate -l 2G /swapfile

If you do not have a swap file, the aforementioned command will create one for you, but in case you already have one, it will overwrite the existing one.

Adjust Swappiness on Linux

Swappiness is a value that the Linux kernel uses to determine how often the RAM should use the swap file. By default, the value is set to 60. The higher the value, the more often your RAM will use the swap file, which is ideal if you have low RAM. Possible swap file values range from 0 to 100.

To view your swappiness value, run the following command:

cat /proc/sys/vm/swappiness

To temporarily change the swappiness value on your system, for example, 20, use the following command:

sudo sysctl vm.swappiness=20

Note: This value will be reset to the default after your system reboots.

To persist your swappiness, you should append the following line vm.swappiness=20 at the bottom of your etc/sysctl.conf configuration file.

Make sure to reboot your system for the changes to take effect.


How to Remove a Swap File

To remove a swap file that you no longer need, first disable it using swapoff and then use the rm command to delete your file.

sudo swapoff /swapfile
sudo rm /swapfile

Using Dedicated Partitions for Swapping

With a swap file in place, you should expect much better performance from your system because a swap file will prevent your system from crashing while running memory-intensive applications.

For mission-critical systems, you might consider creating a dedicated partition on your hard disk for swapping. A swap partition and swap file have their own pros and cons associated with them.

Author: Mwiza Kumwenda

Source: Mwiza Kumwenda.” Getting Started With Swap Files on Linux”. Retrieved From

All Rights Of This Article Reserved To MakeUseOf

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: