Linux Kernel

Manage and monitor swap space on Linux

Most of us don’t think much about swap space unless we run into issues that suggest we don’t have enough systems. However, visualizing and measuring the adequacy of swap space in a system is not that complicated, and knowing the normal state of your system will help you find it if something goes wrong. Now let’s take a look at some commands that can help you find the swap space. But first, let’s go over some basics.

What is swap space and how is it used

Swap space is disk space that acts as an extension of memory. Used when the physical memory (RAM) of the system is full and the system requires more memory resources. This is called an “exchange”. This is so that the system can move inactive pages in memory to swap space to accommodate more data in RAM. That is, it provides a way to free up RAM on a busy system.

Programs and data use RAM. This is because RAM is the only way the system can handle it. In fact, when the system boots, the kernel and systemd In RAM to begin with.

You can configure the swap space as your own disk partition or define it as a file. Nowadays, most Linux installations create partitions during installation, which is preferable. However, you can configure a swap file and use it for swap space.

Insufficient swap space can cause a problem called “thrashing”. This problem slows down the system because programs and data frequently move between RAM and swap space.

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

How much exchange do you need?

The swap space recommendation was twice as much RAM, but came back when the system didn’t have as much RAM as typical RAM today. These Ubuntu recommendations will probably work 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 dormant swap is important. A system in hibernation immediately saves the state of the system to the hard drive and turns it off. Upon startup (for example, by lifting the “lid” of your laptop computer), any programs you were running will revert to the state they were in when the system entered hibernation. Therefore, we recommend more swap space. Not all systems go into hibernation.

To determine if the system can be hibernated, run the following command:

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

If you get the above answer, your system is in hibernation. You can test it by running the following command:

$ sudo pm-hibernate

How can I view the amount of swap space on my Linux system?

You can use swapon – display A command that displays the system swap space.

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

Another useful command is freedom A command that displays both swap space and memory usage. -NS, 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

NOT. sar The command can report the use of the swap space.

$ 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

With the above output freedom A command that indicates swap space is used moderately, even though a large amount of free memory is available.

You can also display the swap partition with a command similar to the following:

$ 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!

If you don’t need more swap space

If your system has a large amount of memory, you may not need to use swap space. However, in most cases it is advisable to have it on hand. Disk capacity is relatively cheap compared to memory, and it is not known when processes can add to the load. On the other hand, if swap space is used almost always frequently, you should consider adding more RAM to your system due to the performance costs associated with its use.

Creating a swap file

If you need to create a swap file on your Linux system, use a command similar to the following:

$ 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 the permissions of the file and mkswap Using the command Exchange on-a With the order to make it available swapon – display A command to verify that it is being 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 deactivate and then reactivate the exchange

You can activate and deactivate the use of swap files, To exchange When Exchange However, you only need to disable swap if you add a swap partition and 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

conclude

If your Linux system is still working fine, you may not have memory or swap issues. However, if you don’t, or if you’re curious about how to set up and use the swap space, try some of the commands above to see what you find out.

Join the global network community Facebook When LinkedIn Comment on the topics that come to mind.

Copyright © 2021 IDG Communications, Inc.


Source link

Leave a Reply

Your email address will not be published. Required fields are marked *