by  Marirs

Swappiness and the Linux Swap

Mostly, if you are on VPS and will want to have a swap file, since there is no partition, its easy to create a swap file and set it for use as a swap space. A swap can occur and a page of memory is copied from the RAM to preconfigured space on the hard disk. However, don’t make the habit of creating a lot of swap files & thrashing your poor VPS.

  • First Check for Swap Space
    [[email protected]]# swapon -s
    If you got an empty list, then you don’t have a swap space configured.
  • Let us Create & enable the Swap File

    Before we create a swap file, let us understand how much of disk space you have to execute this task successfully.

    [[email protected]]# df -H
    Filesystem      Size  Used Avail Use% Mounted on
    udev            1.1G     0  1.1G   0% /dev
    tmpfs           210M   26M  185M  13% /run
    /dev/vda1        48G   11G   35G  23% /
    tmpfs           1.1G     0  1.1G   0% /dev/shm
    tmpfs           5.3M     0  5.3M   0% /run/lock
    tmpfs           1.1G     0  1.1G   0% /sys/fs/cgroup
    /dev/loop0      3.4G  3.4G     0 100% /mnt/win7

    In this we see enough space in the rootfs, so lets create a swapfile there. We will create a swap file of 2GB.

    [[email protected]]# dd if=/dev/zero of=/swapfile bs=1024 count=2097152
    2097152+0 records in
    2097152+0 records out
    2147483648 bytes (2.1 GB) copied, 9.34127 s, 230 MB/s    

    To know how we calculated for 2GB, it is 2048 (2GB) * 1024, essentially bs * space required in mb will give the value of “count”. You should see the above output. Next we are going to prepare the swap file by creating a linux swap area and there by activating it:

    [[email protected]]# mkswap /swapfile
    Setting up swapspace version 1, size = 2097148 KiB no label, UUID=c1ca5702-0c79-48f0-b60a-344587b1959c
    [[email protected]]# swapon /swapfile

    The swapon command tells the kernel to start using it as swap space.

    Now you should be able to see the new swap file when you view the swap summary.

    [[email protected]]# swapon -s
    Filename Type Size Used Priority
    /swapfile file 2097148 0 -1    
  • Make the swap permanent

    To make sure that the swap is permanent we need to include this in the fstab file. To do this; edit the /etc/fstab with your favourite editor

    [[email protected]]# nano /etc/fstab

    and include the below line:

     /swapfile       none    swap    sw      0       0

    Save the file, and you are done getting a swap setup in your VPS. Before we complete, lets do the finishing by changing permissions of the /swapfile by doing the below:

    [[email protected]]# chown root:root /swapfile
    [[email protected]]# chmod 0600 /swapfile
  • Set the swappiness.
    [[email protected]]# echo ‘vm.swappiness=30’ >> /etc/sysctl.conf

Since kernel 2.6.28, Linux uses a Split Least Recently Used (LRU) page replacement strategy. Pages with a filesystem source, such as program text or shared libraries belong to the file cache. Pages without filesystem backing are called anonymous pages, and consist of runtime data such as the stack space reserved for applications etc. Typically pages belonging to the file cache are cheaper to evict from memory (as these can simple be read back from disk when needed). Since anonymous pages have no filesystem backing, they must remain in memory as long as they are needed by a program unless there is swap space to store them to.

Given a vm.swappiness of 100, the priorities would be equal (file_prio=200-100=100, anon_prio=100). This would make sense for an I/O heavy system if it is not wanted that pages from the file cache being evicted in favour of anonymous pages.

Conversely setting the vm.swappiness to 0 will prevent the kernel from evicting anonymous pages in favour of pages from the file cache. This might be useful if programs do most of their caching themselves, which might be the case with some databases. In desktop systems this might improve interactivity, but the downside is that I/O performance will likely take a hit.