Modify Your Swapfile For Better Performance

A swap file is an area on your hard-disk where the computer continuously keeps storing data which is not in immediate use. Now, a drive failure of the swap partition can cause some applications which require high amount of swap memory to crash.

There are two workarounds to this :

  1. Firstly, you can reduce the system swappiness. With this, you can set the system to use less swap memory. The possible values are from 0 to 100.
    Just run the command :

    sysctl vm.swappiness=desired swappiness

    Here, replace ‘desired swappiness’ with a desired value between 0 to 100. 0 indicates no swap usage, thereby running everything on the RAM, unless absolutely necessary, 100 indicates immediate swapping.For this to take effect without a reboot, run

    swapoff -a

    and

    swapon -a

    as root.

  2. Using a swap file.
    Using a swap file has it’s own advantages. A separate swap partition has to be fully functional and error free at all times. Also, it eats up some time of the already slow Linux bootup process. With a swap file, you can adjust it’s size, So, to use a swap file, become root. Then follow the steps below.

    • Create an empty file with the command :
      dd if=/dev/zero of=/swapfile bs=1048576 count=1000
    • Create a swapfile :
      mkswap /swpf
    • Turn it on.
      swapon /swpf
    • Add the line
    • /swapfile    swap   swap  defaults 0

      to the file /etc/fstab for doing this permanently.

  3. Check the creation using :
    swapon -s

That is all. Get a better and faster debian based system in a few seconds, without any restart.

Published by

Chinmoy Kanjilal

Chinmoy Kanjilal is a FOSS enthusiast and evangelist. He is passionate about Android. Security exploits turn him on and he loves to tinker with computer networks. You can connect with him on Twitter @ckandroid.

  • About :
    mkswap /swpf
    swapon /swpf

    I think it should be /swapfile instead of /swpf, am I right ?