Americas

  • United States
sandra_henrystocker
Unix Dweeb

Managing and monitoring swap space on Linux

How-To
Sep 14, 20216 mins
Linux

Swap space can play an important role in system performance. Learn how to determine how much swap space your system has available and how much it's being used.

linux penguin
Credit: Sandra Henry-Stocker

Most of us don’t often think about swap space unless we run into a problem on our systems that suggests we don’t have enough. Even so, viewing and gauging the adequacy of swap space on a system is not overly complicated, and knowing what’s normal for your system can help you spot when something is wrong. So let’s check out some commands that can help you look into your swap space. But first, let’s review some fundamentals.

What swap space is and how it’s used

Swap space is disk space that acts something like an extension of memory. It gets used when the system’s physical memory (RAM) is full and the system needs more memory resources. It’s called “swap” because the system will move some inactive pages in memory into the swap space so that it can accommodate more data in RAM. In other words, it provides a way to free up RAM on a busy system.

Programs and data use RAM because that’s the only way they can be processed by the system. In fact, when a system boots, it moves programs like the kernel and systemd into RAM to get going.

Swap space might be configured as its own disk partition or be set up as a file. These days, most Linux installations create a partition during installation, and this is optimal. You can, however, set up a swap file and use it for your swap space.

With inadequate swap space, you can run into a problem called “thrashing” in which programs and data are moved between RAM and the swap space so frequently that the system runs very slowly.

Together, RAM and swap are referred to as “virtual memory.”

How much swap do you need?

The recommendation for swap space used to be double your RAM, but that was back when systems didn’t have as much RAM as they generally have today. These recommendations for Ubuntu should probably work well for other distributions as well:

RAM         Swap        Swap (with hibernation)
256MB       256MB       512MB
512MB       512MB       1GB
1GB         1GB         2GB
2GB         1GB         3GB
3GB         2GB         5GB
4GB         2GB         6GB
6GB         2GB         8GB
8GB         3GB         11GB
12GB        3GB         15GB
16GB        4GB         20GB
24GB        5GB         29GB
32GB        6GB         38GB
64GB        8GB         72GB
128GB       11GB        139GB

The distinction between swap and swap with hibernation is important. A system that hibernates saves your system state immediately to the hard disk and powers down. When you wake it up (e.g., by lifting the “lid” on a laptop), all the programs you were running return to the state they were in when the system went into hibernation. So, more swap space is recommended. Not all systems hibernate.

To determine if your system can hibernate, run this command:

$ which pm-hibernate
/usr/sbin/pm-hibernate

If you get the response shown above, your system is hibernation-ready. You can test it by running this command:

$ sudo pm-hibernate

How you can view the amount of swap space on your Linux system?

You can use the swapon –show command to view the swap space on your system.

$ swapon --show
NAME       TYPE      SIZE USED PRIO
/dev/zram0 partition 5.8G 3.3M  100

Another useful command is the free command that displays both swap space and memory usage. With -m, the results are displayed in MBs instead of KBs.

$ free
               total        used        free      shared  buff/cache   available
Mem:         6064768      740736      538288        8060     4785744     5014712
Swap:        6064124        3328     6060796
$ free -m
               total        used        free      shared  buff/cache   available
Mem:            5922         723         525           7        4673        4897
Swap:           5921           3        5918

The sar command can report on swap space usage.

$ sar -S 1 3
Linux 5.13.9-200.fc34.x86_64 (dragonfly)        09/10/2021      _x86_64_       (2 CPU)

02:09:55 PM kbswpfree kbswpused  %swpused  kbswpcad   %swpcad
02:09:56 PM   6060796      3328      0.05         0      0.00
02:09:57 PM   6060796      3328      0.05         0      0.00
02:09:58 PM   6060796      3328      0.05         0      0.00
Average:      6060796      3328      0.05         0      0.00

Notice in the above output from the free command that swap space is being modestly used even though a lot of free memory is available.

You can also view a swap partition with a command like this:

$ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0    7:0    0  32.3M  1 loop /var/lib/snapd/snap/snapd/12704
loop1    7:1    0  55.4M  1 loop /var/lib/snapd/snap/core18/2128
loop2    7:2    0  65.4M  1 loop /var/lib/snapd/snap/powershell/173
loop3    7:3    0  32.3M  1 loop /var/lib/snapd/snap/snapd/12883
sda      8:0    0 111.8G  0 disk
├─sda1   8:1    0     1G  0 part /boot
└─sda2   8:2    0 110.8G  0 part /
sdb      8:16   0 465.8G  0 disk
└─sdb1   8:17   0   434G  0 part /home
sdc      8:32   1   1.9T  0 disk
└─sdc1   8:33   1   1.9T  0 part
sr0     11:0    1  1024M  0 rom
zram0  252:0    0   5.8G  0 disk [SWAP]              

When you do and don't need more swap space

If your system has a lot of memory, you may never need to use swap space. But it's nearly always a good idea to have it available. Disk space is relatively cheap compared to memory, and you never know when some process might add to the burden. On the other hand, if your swap space is heavily used nearly all of the time, you should maybe consider adding more RAM to the system as there is some performance cost associated with its use.

Creating a swap file

If you need to create a swap file on a Linux system, use a command like this:

$ sudo dd if=/dev/zero of=/swapfile bs=1M count=8192
[sudo] password for me:
8192+0 records in
8192+0 records out
8589934592 bytes (8.6 GB, 8.0 GiB) copied, 147.893 s, 58.1 MB/s

Once the file is created, change its file permissions, run the mkswap command and use the swapon -a command to make it available and the swapon --show command to verify that it has been put into use.

$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 8 GiB (8589930496 bytes)
no label, UUID=3d060a1d-90d1-436f-97b6-4d1aebb15ce2
$ sudo swapon -a
$ swapon --show
NAME      TYPE SIZE USED PRIO
/swapfile file   8G   0B   -2

How to turn swapping off and back on again

It is possible to turn use of a swap file on and off and using the swapoff and swapon commands, though you'd probably only want to turn swapping off if you added a swap partition and want to use it instead of the swap file.

$ sudo swapoff -v /swapfile
swapoff /swapfile
$ sudo swapon -v /swapfile
swapon: /swapfile: found signature [pagesize=4096, signature=swap]
swapon: /swapfile: pagesize=4096, swapsize=8589934592, devsize=8589934592
swapon /swapfile
$ swapon –show
NAME      TYPE SIZE USED PRIO
/swapfile file   8G   0B   -2

Wrap-up

If your Linux system runs smoothly all of the time, it likely isn't having any problems with memory or swap. However, if it isn't or if you're just curious about how swap space is set up and used, try some of the commands above to see what they can tell you.

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.