Wednesday, January 21, 2015

Linux - swap

Swap is the area on a hard drive (can be a logical volume partition) which is part of virtual memory of your server. It holds inactive memory pages temporarily. Swap space is used when the amount of RAM is insufficient or full. When OS runs out physical memory, it is unbale to start new processes or allocate more memory to existing processes. When this happens, if you have a swap partition configured, OS will "swaps" inactive processes (pages) out of memory and on to swap partition.

Even though swap space can be used as "extra" RAM, but the access time for swap is slower than RAM therefore it should not be considered to be a replacement for the RAM.

Swap space can be a dedicated swap partition, a swap file, or a combination of swap partitions and swap files.

Do I need swap?

Technically, it is not necessary. Linux can run without swap partition. If OS gets into a situation where it doesn't have sufficient memory, it will panic and stop running. But in practical, it is necessary. Although swapping slows down system and having as much as RAM on the server is the best solution to avoid swapping, there are legitimate situations where swapping is desirable. For example, during system boot, there can be processes that run but then effectively sleep for a long time. Or memory that is allocated, written to and then never used again. In these situations, the swapping algorithm will start to migrate this memory to the hard disk and so free up valuable system memory. This ensures the maximum amount of RAM is available at all times and so increase performance. So, in a conclusion, it is always good to have a swap partition.

How much swap space do I need?

Normally swap should equal 2x physical RAM for up to 2 GB of pyhsical RAM, and then an additional 1x physical RAM for any amout above 2GB, but never less than 32 MB.

if memory < 2GB
    swap = M * 2
if swap < 32MB
    swap = 32MB
else
    swap = M + 2GB

So a system with 2GB of RAM would have 4GB of swap, a system with 3GB of RAM would have 5GB. For systems with really large RAM (>= 32G), you can have a smaller swap partition (<= 1x RAM).

How to check the swap space usage?

There are couple of ways that you can check swap usage.

Check how much swap space been used/free:
"free -m"
$ free -m
             total       used       free     shared    buffers     cached
Mem:         64377      46178      18199          0        115      29122
-/+ buffers/cache:      16941      47436
Swap:         2047       2025         22

Numbers are in MB. In the above example, the system has a total of 2047MB swap space, used 2025MB, and have 22MB left.

Check wich process is using swap:
Here is a bash script to help you check which process is using swap space (check-swap.sh). Run as root.

Sample output:
$ sudo ./check-swap.sh
............................................................
Overall swap used: 2075020 kb
========================================
kb       pid   name
========================================
1841592 22021 java
68528   19654 java
54192   7894  dsm_om_connsvcd
27640   18687 java
17848   18662 miniserv.pl
12296   12897 java
...

kb = kilobits

Is the system really swapping?:

Seeing swap space occupied doesn't mean OS is doing swapping. As I mentioned earlier, there are situations like: during system boot, there can be processes that run but then effectively sleep for a long time. Or memory that is allocated, written to and then never used again. In these situations, the swapping algorithm will start to migrate this memory to the hard disk and so free up valuable system memory. This ensures the maximum amount of RAM is available at all times and so increase performance.

You can check the swap activity by "sar" command:
$ sudo sar -W 60 5
04:23:10 PM  pswpin/s pswpout/s
04:24:10 PM      0.00      0.00
04:25:10 PM      0.00      0.00
04:26:10 PM      0.00      0.00
04:27:10 PM      0.00      0.00
04:28:10 PM      0.00      0.00
Average:         0.00      0.00

-W: Report swapping statistics. The following values are displayed:
pswpin/s
    Total number of swap pages the system brought in per second.
pswpout/s
    Total number of swap pages the system brought out per second.

In the above case, swap space are allocated but there is no activities.

One important kernel parameter "Swappiness":

The Linux kernel can be tweaked to define how aggressively it should try to swap processes out of memory. This tendency is controlled by a kernel variable called swappiness. A swappiness of 0 means that the kernel will avoid swapping as much as possible, while 100 means the kernel will be aggressive in how it uses the swap space. The default setting for many Linux distributions is 60. It is possible to change the number dynamically using this command:

$ sudo sysctl vm.swappiness=10

To permanently set a value, you need to change (or add, if it doesn’t exist) the vm.swappiness variable in the /etc/sysctl.conf file.

No comments: