Swapping is costly for disk I/O and not every Linux server needs to have a swap partition and to start swapping. For instance, MySQL servers have more than enough RAM available to do their work. Yet, when a swap partition is enabled Linux starts swapping, and that degrades MySQL database performance.

Bash script to turn off Linux swap partition

An unwanted Linux swap partition can be the result of an automated and unattended Linux installation. Of course you can fiddle with the Linux kernel swapiness settings, located in /proc/sys/vm/swapiness and configurable in /etc/sysctl.conf, but one can turn off the swap completely too.

Here's a tiny bash script to disable Linux' swap - for which we use the swapoff command - and to comment out the swap partition in /etc/fstab

#!/bin/bash
# swapoff -a to disable swapping
swapoff -a
# sed to comment the swap partition in /etc/fstab
sed -i.bak -r 's/(.+ swap .+)/#\1/' /etc/fstab

Save this as turn_swap_off.sh and execute as root:

sh /root/turn_swap_off.sh

A backup of the /etc/fstab file is made just to be sure and to stay on the safe side.

Disable swappiness kernel parameter

Swappiness is a property for the Linux kernel that changes the balance between swapping out runtime memory, as opposed to dropping pages from the system page cache. Swappiness can be set to values between 0 and 100, inclusive. A low value means the kernel will try to avoid swapping as much as possible where a higher value instead will make the kernel aggressively try to use swap space.

You manage swappiness with sysctl, either to get its current value or to set a new swappiness value. You can also use cat to verify the current setting. For example:

# get current swappiness value using cat
$ cat /proc/sys/vm/swappiness
60
# get current swappiness value using sysctl
$ sudo sysctl vm.swappiness
[sudo] password for user:
vm.swappiness = 60
# set a new swappiness value to 10
$ sudo sysctl vm.swappiness=10

By default, the vm.swappiness value on most Linux computers is set to 60. The higher the number, the more aggressively the VM swaps data to disk. You can change this level to a different setting either temporarily or indefinitely.

You can also edit the /etc/sysctl.conf configuration file. Open the file /etc/sysctl.conf with your text editor and change the value of the following entry vm.swappiness to your suitable value (add the entry if it does not exist).

$ sudo vi /etc/sysctl.conf
$ grep swappiness /etc/sysctl.conf
vm.swappiness=10
Donate a cup of coffee
Donate a cup of coffee

Thank you very much! <3 ❤️

2 Comments

  1. Hi Jan,

    Instead of using a complete partition as swap. You can also use a swapfile which is much more flexible. You can even use more than one swapfile on other devices. It makes your systems much more responsive. Without getting stuck on low free RAM.

    # dd if=/dev/zero of=/swapfile bs=1M count=512
    # chmod 600 /swapfile
    # mkswap /swapfile
    # swapon /swapfile

    Edit the /etc/fstab to auto-enable the swapfile:
    /swapfile none swap defaults 0

    and for removing it:

    # swapoff -a
    # rm -f /swapfile
    • Hi Brian, thank you for your comment :)

      Great point! However, in my opinion – and with current amounts of RAM available in systems – the need for swap often indicates a problem elsewhere. And on mission critical systems like our MySQL servers, you don’t want degraded performance because of swapping data in and out.

      OTOH, if you have RAM to spare, here’s how to create a nifty RAM disk in Linux:
      https://www.saotn.org/linux-ramdisk-mini-howto/ :-)

Leave a Reply

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