Paging and Swap Space

Linux Swap Space and Paging Explained

What is swap?


Swap space or paging space is an area of disk that is used for storage of memory that has been swapped out (paged out) of RAM. Linux's memory is divided into chunks of memory called pages. Swapping is the process where Linux moves the contents of memory to a preconfigured area of disk called a swap space. Swapping generally takes place when more memory is required than is physically available. It is the responsibility of the kernel to swap out these less used pages to disk and free memory for process that require the memory immediately.

Although swapping can help your system through a time of intensive memory use, it should not be seen as a replacement for physical memory. Physical memory can be accessed far faster than the storage on a swap partition.

If you system continually reports heavy swapping, you will notice a deterioration of system performance. We will cover the monitoring of swap usage a little later.



To view available swap space on your system, you can issue one of the following commands:

cat /proc/swaps



[root@centos01 ~]# cat /proc/swaps
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       1540088 0       -1

swapon -s



[root@centos01 ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       1540088 0       -1

From the above command we can see that our swap space is of the type partition. We have 1504MB of available space (1540088Kb). No swap space has been currently utilised. We can also set a priority here. The priority is useful if you have more than one swap space. You can specify which device to use first or set both devices to the same priority. When you set the priorities the same, your system will spread its paging across the two devices thus improving performance. It is possible to have upto 32 swap spaces defined on a Linux system!


free command


The free command is used to display the amount of free and used memory within your system. The following options are available:

free -k

Display memory usage in Kilobytes:



[root@centos01 ~]# free -k
             total       used       free     shared    buffers     cached
Mem:        511100      90220     420880          0       6028      33988
-/+ buffers/cache:      50204     460896
Swap:      1540088          0    1540088

free -m

Displays memory usage in Megabytes:



[root@centos01 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:           499         88        411          0          5         33
-/+ buffers/cache:         49        450
Swap:         1503          0       1503

Creating a new swap space


In this example I am adding a new disk of 1GB in size. (This has been added under VirtualBox)

To view my new disk I need to issue the command: fdisk -l

The relevant output from the above command is as follows:



Disk /dev/sdd: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Here we can see that we have a new disk /dev/sdd of 1073MB in size. before we can use this disk, we need define this as a swap partition. This can be achieved by using the fdisk partitioning tool:



[root@centos01 ~]# fdisk /dev/sdd

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-130, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130):
Using default value 130

Command (m for help): t
Selected partition 1

Hex code (type L to list codes): L

Hex code (type L to list codes): 82
Changed system type of partition 1 to 82 (Linux swap / Solaris)

Command (m for help): p

Disk /dev/sdd: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x79308d6b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdd1               1         130     1044193+  82  Linux swap / Solaris

Now we need to write our changes to disk. To accomplish this we select "w" for write changes:



Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Create a swap space with the mkswap command


The command to issue is: mkswap /dev/sdd1



[root@centos01 ~]# mkswap /dev/sdd1
Setting up swapspace version 1, size = 1044188 KiB
no label, UUID=4bab3abf-3a24-433e-b74a-bc5646dec96b

Activate swap space using swapon command



[root@centos01 ~]# swapon /dev/sdd1

Verify new swap device



[root@centos01 ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       1540088 0       -1
/dev/sdd1                               partition       1044184 0       -2

The negative values from the priority are assigned automatically if you do not specify a priority on the swapon command. The "-1" is higher than "-2" so this will be used first. If you manually assign a priority, this will be a positive number between 0 and 32767 Below is an example of how to change your priorities:



[root@centos01 ~]# swapoff /dev/sdd1
[root@centos01 ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       1540088 0       -1

The above command: swapoff /dev/sdd1 removed our swap file.



[root@centos01 ~]# swapon -p 0 /dev/sdd1
[root@centos01 ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       1540088 0       -1
/dev/sdd1                               partition       1044184 0       0

The above command: swapon -p 0 /dev/sdd1 command sets the swap priority to "0". This is a higher number than "-" so will now be used first!

Although the swapon command is very useful, you still need to add your new swap partition to the mount table "/etc/fstab"



Adding swap to your mount table


Add the following entry to your mount table:



/dev/sdd1                swap                   swap    pri=0,defaults          0 0

Notice the prefix of "pri=0"

To test our entry within the mount table, we can do the following:



[root@centos01 ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       1540088 0       -1
/dev/sdd1                               partition       1044184 0       0

The above indicates that we currently have two swap areas defined. Now lets switch one of these off:



[root@centos01 ~]# swapoff /dev/sdd1
[root@centos01 ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       1540088 0       -1

From the above output you can see that our new swap area has been removed. We will now issue the command: swapon -a to mount any swap entries within the mount table:



[root@centos01 ~]# swapon -a
[root@centos01 ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       1540088 0       -1
/dev/sdd1                               partition       1044184 0       0

We can now see our entry has been automatically mounted. This means that it will also be available after a reboot of your system.


What is swappiness?


Swappiness is a parameter that controls the frequency the kernel will try to move files out of physical memory to disk. On most distributions this value is set to "60" (Ubuntu and CentOS have a value of 60) The value is a number between 0 and 100. The value "0" for swappiness would instruct the kernel to refrain from swapping processes out of memory for as long as possible. A value of "100" for swappiness would instruct the kernel to aggressively swap processes out of physical memory to disk.

To verify what setting you have on your distribution, you can issue the command:

cat /proc/sys/vm/swappiness



[root@centos01 ~]# cat /proc/sys/vm/swappiness
60

To change this parameter we can issue the command:



[root@centos01 /]# echo 55 > /proc/sys/vm/swappiness

[root@centos01 /]# cat /proc/sys/vm/swappiness
55

Adding vm.swappiness into sysctl.conf


To make this change permanent, you will need to add the following line to your "/etc/sysctl.conf" configuration file:



# Modified Swappiness Parameter
vm.swappiness=55

Setting swappiness under Ubuntu


Under Ubuntu the process is almost the same. To determine your current value, we can issue the same command: cat /proc/sys/vm/swappiness

You should see the familiar value of "60".

If you want to change this temporarily, you can issue the following command: sudo sysctl vm.swappiness=55

To make this change permanent you will need to add the following entry into "/etc/sysctl.conf":



# Modified Swappiness Parameter
vm.swappiness=55