Most of us don’t think about swap space often unless we run into a problem on our systems that suggests we don’t have enough. Even so, seeing and measuring the adequacy of swap space on a system isn’t too difficult, and knowing what’s normal for your system can help you spot errors. So let’s look at some commands that can help you examine your swap space. But first, let’s cover some basics.
What is swap space and how is it used
Swap space is hard disk space that acts like an extension of the main memory. It is used when the system’s physical memory (RAM) is full and the system needs more memory resources. It’s called “swap” because the system moves some inactive pages in memory to swap so it can hold 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 is the only way the system can process them. In fact, when a system boots, programs like the kernel and systemd into RAM to get started.
The swap memory can be configured as a separate hard disk partition or set up as a file. Most Linux installations these days create a partition during the installation, and this is optimal. However, you can set up a swap file and use it for your swap area.
Insufficient swap space can cause a problem called “thrashing”, in which programs and data are moved between RAM and the swap area so frequently that the system runs very slowly.
RAM and swap are collectively referred to as “virtual memory”.
How much change do you need?
The recommendation for swap space used to be to double the amount of memory, but that was back when systems didn’t have as much memory as they do 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 hibernation swap is important. A system in hibernation immediately saves your system status to the hard drive and shuts down. When you wake it up (for example, by lifting the “lid” of a laptop), any programs you were running return to the state they were in when the system went to sleep. Therefore, more swap space is recommended. Not all systems hibernate.
Run this command to see if your system can be put to sleep:
$ which pm-hibernate /usr/sbin/pm-hibernate
When you get the answer shown above, your system is ready to go to sleep. You can test it out by running this command:
$ sudo pm-hibernate
How can you view the amount of swap space on your Linux system?
You can use the … swapon –show Command to display the swap area on your system.
$ swapon --show NAME TYPE SIZE USED PRIO /dev/zram0 partition 5.8G 3.3M 100
Another useful command is the for free Command that shows both swap space and memory usage. With -m, the results are displayed in MB instead of KB.
$ 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 of for free Command that swap space is used modestly, although there is a lot of free space available.
You can also display 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] <=== there it is!
When you need 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 almost always a good idea to have it available. Storage space is relatively cheap compared to memory, and you never know when a process might add to its load. On the other hand, if your swap space is being used heavily most of the time, you might want to consider adding more RAM to the system as there is some performance penalty associated with using it.
Create swap file
If you need to create a paging 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
After 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 check if it was used.
$ 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 on again
It is possible to switch the use of a paging file on and off and the use of the exchange and To deceive Commands, although you probably only want to turn off paging if you’ve added a swap partition and want to use that instead of the paging 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
If your Linux system is running smoothly all the time, it likely doesn’t have any memory or swap issues. However, if it doesn’t, or you’re just curious about how to set up and use swap space, try some of the commands above to see what they can tell you.
Copyright © 2021 IDG Communications, Inc.