Memory and Swap Usage in Linux

The article below is the English translation of the original Turkish post I wrote in 2013.

In situations where your computer’s RAM is insufficient, Linux uses a designated portion of the hard drive as virtual memory. This ensures the system doesn’t crash or that your computer can resume operations seamlessly after being put into sleep mode. However, hard drives are significantly slower compared to RAM. To put this into perspective, RAM is at least 100 times faster than hard drives (and in some cases up to 400 times faster).

For a clearer understanding, you can watch a comparison of RAM and hard drive read/write speeds:

When your computer begins to use virtual memory, you’ll notice a significant drop in system performance.

Linux prevents system crashes by utilizing a portion of the hard drive as RAM when physical memory runs out. This portion of the hard drive is called the swap space.

When you first install Linux and check RAM usage through the System Monitor, you might notice that swap space is being used even though there’s still free RAM. For example, in the screenshot below, a system with 3.7 GB of total RAM shows swap usage despite 35% of the RAM being available.

This raises an interesting question:

Why is swap space being used while there’s still free RAM?

Years ago, computers had very limited RAM. Think back to the days when computers with 256 MB or 512 MB of RAM were the norm.

Since Linux is designed to run even on low-end hardware, default settings are optimized for systems with limited RAM. However, for systems with sufficient memory (e.g., 4 GB, which we consider adequate here), you can tweak a parameter in the Linux kernel called vm.swappiness to prevent unnecessary swap space usage. By making a simple adjustment, we can ensure Linux doesn’t use the hard drive as memory unnecessarily, which in turn will improve overall system performance.

Understanding the “vm.swappiness” Parameter

This parameter controls the relationship between RAM and swap usage. Its default value is 60.

  • The higher the vm.swappiness value, the more Linux will prefer using the hard drive (swap space) as memory.
  • The parameter ranges from 0 to 100:
    • A value of 0 tells Linux to use only RAM, and to use swap space only as a last resort when RAM is completely full.
    • A value of 100 makes Linux use swap space as much as possible.

If you have sufficient RAM, you can instruct the Linux kernel to use swap space only when RAM is entirely full by setting vm.swappiness to 0. Here’s how:

Temporarily Adjusting vm.swappiness

Run the following commands as the root user in the terminal:

echo 0 > /proc/sys/vm/swappiness

or

sysctl -w vm.swappiness=0

This tells the Linux kernel to avoid using swap space unless absolutely necessary. As a result, your system will avoid unnecessary slowdowns caused by swap usage.

Making the Change Permanent

To make this adjustment permanent, you should create a custom configuration file in the /etc/sysctl.d/ directory. This approach ensures that your changes won’t be overwritten when the system updates (directly editing the /etc/sysctl.conf file is not recommended).

Open a new configuration file with the following command:

sudo nano /etc/sysctl.d/my-configuration.conf

Add the following line to the file:

vm.swappiness=0

Save and exit by pressing Ctrl + X, then Y, and finally Enter.

Apply the changes without rebooting by running:

sudo sysctl --system

By making this adjustment, Linux will stop using swap space unnecessarily, allowing your system to run more efficiently. If you have sufficient RAM, this simple tweak can lead to noticeable performance improvements.

Bonus: Manually Moving Swap to RAM

If you haven’t set the vm.swappiness value to 0 but want to manually move everything from the swap space back to RAM, you can use this method.

To manually move the swap content back to RAM, you can follow these steps:

Turn off swap space:

sudo swapoff -a

Wait until it is done.

Turn swap space back on:

sudo swapon -a

This forces the system to move data from the swap space back to RAM, provided there is enough free RAM available.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.