Managing and monitoring swap on Linux

Introduction

This tutorial covers how to manage and monitor swap on Linux, including how to determine the amount of swap space available and how much is currently in use.

Swap space plays an important role in system performance, so it’s crucial to understand how to manage it effectively.

Managing and monitoring swap on Linux

How much swap on Linux do you need?

The recommendation is to swap the space used to double your RAM. If your system has a lot of memory, you may never need to use swap space.

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

To determine if your system can hibernate

$ which pm-hibernate

You can test it by running this command:

$ sudo pm-hibernate

View the amount of swap space on Linux

$ swapon --show
$ free -m 
$ sar -S 1 3
$ lsblk

The output terminal is as follows

[vagrant@localhost ~]$ swapon --show
NAME      TYPE SIZE USED PRIO
/swapfile file   2G   9M   -2
[vagrant@localhost ~]$ free -m
              total        used        free      shared  buff/cache   available
Mem:            486         146          93           2         247         325
Swap:          2047           9        2038
[vagrant@localhost ~]$ sar -S 1 3
Linux 3.10.0-1127.el7.x86_64 (localhost.localdomain)    09/13/2021      _x86_64_        (1 CPU)

09:51:08 PM kbswpfree kbswpused  %swpused  kbswpcad   %swpcad
09:51:09 PM   2087884      9264      0.44       416      4.49
09:51:10 PM   2087884      9264      0.44       416      4.49
09:51:11 PM   2087884      9264      0.44       416      4.49
Average:      2087884      9264      0.44       416      4.49
[vagrant@localhost ~]$ lsblk
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda      8:0    0  40G  0 disk
└─sda1   8:1    0  40G  0 part /
[vagrant@localhost ~]$

Creating a swap file

$ sudo dd if=/dev/zero of=/swapfile bs=1M count=8192
$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile
$ sudo swapon -a
$ swapon --show

Turn to swap off.

$ sudo swapoff -v /swapfile

# The output terminal
[vagrant@localhost ~]$ sudo swapoff -v /swapfile
swapoff /swapfile
[vagrant@localhost ~]$

Turn to swap on Linux.

$ sudo swapon -v /swapfile
$ swapon --show

Conclusion

You have a Managing and monitoring swap on Linux. I hope will this your helpful. Thank you for reading the DevopsRoles page!

,

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.