Linux System Administration 101 - Swap

What is Swap?

Swap, swap space, or swap memory, is a Linux feature which allows you to allocate space on your hard disk to be used as virtual memory.

Virtual memory is a combination of RAM and disk space that processes can use; so the portion of the hard disk set aside for this purpose is called the swap space.

Virtual memory acts similar to memory on the RAM (termed physical memory), except it is, of course, on the hard disk.

Physical memory (RAM) is divided into pages. When a page has not been accessed recently (are deemed inactive), the page can be stored on disk until an application calls for it again, where it will be paged back into RAM.

All these read/write operations on disk means swap is accessed much more slowly, thus less efficient than RAM. Swap should only be used to as a buffer to cater for brief, temporary spikes in memory usage.

Swap space should not be used as an inexpensive alternative to upgrading to a server with more RAM. If your server is always using swap space, it's time to install more RAM. RAM is so cheap nowadays there's no excuse not to.

Allocating Swap

There are two ways to allocate swap:

  1. As a swap partition - an entirely separate portion of the hard disk dedicated to swapping. Requires repartitioning a volume on the hard drive, or
  2. As a swap file - a file set aside to be used as swap

For now, we will only deal with setting up swapping using a swap file.

Swap file

You'd need to prepend your commands with sudo to perform these actions, which requires root priviledges.

How much swap?

The most popular opinion out there advises you to allocate twice the amount of RAM you have on your machine, but, ultimately, it all depends. If you have 2GB of RAM, set a 4GB swap space; if you have 16GB, 8GB might already be enough.

Here, we are going to create an empty file (1GB.swp) 1GB in size, to be used for swap.

Creating Empty File

There are three different ways to allocate space for files - fallocate, dd and truncate.

fallocate allocates, or reserves, space on disks and assigns it to a file. The space is reserved but you don't have to write anything to the file if you don't want to. This is the preferred method.

$ fallocate -l 1G /mnt/1GB.swap
$ cd /mnt/ && ll
total 1048588
drwxr-xr-x  2 root root       4096 Jun  9 09:48 ./
drwxr-xr-x 22 root root       4096 Jun  9 09:41 ../
-rw-r--r--  1 root root 1073741824 Jun  9 09:48 1GB.swap

dd is designed to convert and copy files. What it does here is read from /dev/zero and writes to the file, essentially writing 1073741824 bits worth of 0's to the file. Using dd is much slower because it involves a lot of reads and write; in the end result is the same.

Since fallocate is not supported in all Linux distributions, we can use dd as a 'fallback' method.

$ dd if=/dev/zero of=/mnt/1GB.swap bs=1024 count=1048576
1048576+0 records in
1048576+0 records out
1073741824 bytes (1.1 GB) copied, 11.0973 s, 96.8 MB/s
$ cd /mnt/ && ll
total 1048588
drwxr-xr-x  2 root root       4096 Jun  9 09:52 ./
drwxr-xr-x 22 root root       4096 Jun  9 09:41 ../
-rw-r--r--  1 root root 1073741824 Jun  9 09:53 1GB.swap

There's also /dev/random, which is a special device file that gneerates pseudorandom numbers. You may want to use that instead of /dev/zero, but it's going to be even slower.

truncate creates a sparse file, which means the filesystem stores only the metadata of the file, saying the file is of a certain size, without the memory actually being allocated. So you can use this command to, seemingly, create a file larger than is available.

Do not use truncate to assign swap files.

Formatting File

We can use cat /proc/swaps too see where our swap files are located.

$ cat /proc/swaps
Filename                Type        Size    Used    Priority

Alternatively, you can run swapon -s instead of cat /proc/swaps

At the moment, even though we fallocate the file, it's just a random file - the machine doesn't recognize it as being used for swap. So next, we need to format the file to be a swap file.

$ mkswap /mnt/1GB.swap
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=ea916f0c-bcdf-4489-b455-4f7fe50108d2

Turn Swap On

Lastly, we need to add the newly-formatted file to the system

$ swapon /mnt/1GB.swap

Now if we run cat /proc/swaps again, we can see the swap file.

$ cat /proc/swaps
Filename                Type        Size    Used    Priority
/mnt/1GB.swap                           file        1048572 0   -1

You may have also noted the the Type column is set to file, because we are using a swap file.

Swap on Boot

At the moment, whenever the machine is rebooted, swap is disabled. We must add the following entry to /etc/fstab to ensure it is enabled at boot time.

/mnt/1GB.swap none swap sw 0 0

Alternatively, simply run echo “/mnt/1GB.swap none swap sw 0 0” >> /etc/fstab

Monitoring Swap

free

We can use the free command to see memory usage at the moment. For a machine not running many processes, swap space is not used at all.

# free
             total       used       free     shared    buffers     cached
Mem:        501800     495068       6732        332       1104     443512
-/+ buffers/cache:      50452     451348
Swap:      1048572          0     1048572

If I use stress to increase memory usage, you can see swap are being used.

# stress --cpu 8 --io 4 --vm 2 --vm-bytes 512M --timeout 10s
stress: info: [1874] dispatching hogs: 8 cpu, 4 io, 2 vm, 0 hdd
stress: info: [1874] successful run completed in 11s

# free
             total       used       free     shared    buffers     cached
Mem:        501800      37264     464536          8       1056       5848
-/+ buffers/cache:      30360     471440
Swap:      1048572       8376    1040196
vmstat
# vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0  15272   4604 431108    0    0    21   145    6    8  0  0 100  0  0

The columns si and so shows the amount of memory paged in from disk and paged out to disk, respectively. A high number means many pages are being swapped in and out - this is a bad sign.

After being stressed, here's the readings:

# vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0   8376 464520   1060   5848    0   22    31   167    7   25  0  0 99  0  0

You can also try:

  • cat /proc/meminfo

Swappiness

If swap is available, the Linux kernel will move some inactive processes into swap to clear out memory in RAM for more active processes. How aggressively the kernel does this is determined by the swappiness value. A value of 100 will aggressively move process to swap, while a value of 0 will utilize swap only when not doing so will cause out-of-memory errors.

To configure the swappiness (aggresiveness) temporarily, run

$ sysctl vm.swappiness=10

To configure swappiness permanently, add to /etc/sysctl.conf the following line:

vm.swappiness=[0-100]

Replacing [0-100] with a number between 0 and 100. The default on Ubuntu Desktop is 60, for the server, you might want a lower value, say 10.

To check the current swappiness value from the command line, run

$ cat /proc/sys/vm/swappiness
60

Cloud Servers

Another caveat of swap is it causes more read and write operations per second, because swap space is on disk, leading to a high disk IOPS. In a cloud environment, where resources are shared, a virtual server which utilizes swap heavily may have a high disk IOPS, slowing down other users by doing so. Thus, some cloud providers would discourage the use of swap.

If you enjoyed this article, why not check out the rest of the series - System Administration 101!

comments powered by Disqus