Check swap usage in linux

Find Swap Memory Usage by Processes in Linux

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

1. Overview

In this tutorial, we’ll discuss how we can check for processes that use swap memory. First, we’ll cover the /proc directory to see what it contains and how we can extract processes’ information from it. Afterward, we’ll write a shell script that will automatically extract the swap usage information used by the processes.

Last, we’ll use the smem tool as an alternative to check for processes that use the swap memory.

2. The /proc Directory

On Linux, proc is a special directory that contains information about the Linux kernel, its configuration, and processes loaded into the physical memory. The operating system creates this directory once we boot into the Linux machine. Moreover, the virtual files inside this directory have no physical size on the disk. The information inside these files is generated on the fly once we read them.

Now that we have an idea about the proc directory, let’s see what’s inside it:

$ ls -l /proc total 0 dr-xr-xr-x 9 root root 0 Feb 17 15:35 1 dr-xr-xr-x 9 root root 0 Feb 17 15:35 10 dr-xr-xr-x 9 root root 0 Feb 17 15:35 100 . dr-xr-xr-x 9 hey hey 0 Feb 17 22:12 34160 . 

As we review the list of items inside this directory, we’ll notice that there are a lot of numbered directories, named directories, and other virtual files. However, we’re only concerned with the numbered directories because they correspond to each running process. The number of the directory is actually the PID of the process.

Читайте также:  Linux convert images to gif

As we can see in the above snippet, it also lists the user processes. So, let’s list the items inside the “34160” directory to see what it contains:

$ ls -l /proc/34160 total 0 . -r--r--r-- 1 hey hey 0 Feb 17 22:12 stat -r--r--r-- 1 hey hey 0 Feb 17 22:12 statm -r--r--r-- 1 hey hey 0 Feb 17 22:12 status . 

The virtual files inside the directory will contain a plethora of information regarding the process. But in our case, we’re interested in the status virtual file because it contains the memory usage information.

Since we’re only interested in the swap usage, we’ll grep the VmSwap field from the file:

$ cat /proc/34160/status | grep VmSwap VmSwap: 0 kB

Now that we know how to check for swap memory used by a process, we can write a shell script to print a list of processes using the swap memory.

2.1. Shell Script to List Processes using Swap Memory

The shell script will go through each numbered directory in the proc directory and print the swap usage it extracts from the status file. It’ll also print the name and PID of the process as well:

#!/bin/bash overall=0 for status_file in /proc/4*/status; do swap_mem=$(grep VmSwap "$status_file" | awk '< print $2 >') if [ "$swap_mem" ] && [ "$swap_mem" -gt 0 ]; then pid=$(grep Tgid "$status_file" | awk '< print $2 >') name=$(grep Name "$status_file" | awk '< print $2 >') printf "%s\t%s\t%s KB\n" "$pid" "$name" "$swap_mem" fi overall=$((overall+swap_mem)) done printf "Total Swapped Memory: %14u KB\n" $overall 

The script is pretty self-explanatory. Let’s run it in the terminal:

$ ./swp.sh 1658 Discord 41448 KB 1681 Xwayland 15772 KB 18621 firefox 213204 KB 18872 WebExtensions 60912 KB . Total Swapped Memory: 1177212 KB

We can easily customize the output further by using something like the column tool.

3. Using the smem Utility

The smem tool displays memory usage of processes. Apart from showing the RSS, PSS, and USS memory, it can also show the swap memory.

By default, it doesn’t ship with most Linux distributions. So, we’ll have to install it from our distribution’s official repository.

3.1. Installation

The smem utility will be available under the smem package name. We can use a package manager like yum or apt to install it.

For Debian and its derivatives:

For Fedora, OpenSUSE, and RHEL:

3.2. Usage

Once smem is installed, we can try it out in the terminal:

$ smem PID User Command Swap USS PSS RSS 494 hey swaybg -o DVI-I-1 -i /home/ 15136 4 86 2304 1665 hey /opt/discord/Discord --type 8800 0 96 820 589 hey /usr/lib/pulse/gsettings-he 1224 20 122 2372 37348 hey /bin/sh /usr/bin/android-st 204 164 180 1220 . 

Let’s say we want to print the top 10 processes sorted by swap memory usage in descending order. We can do so with the -s or –sort option:

$ smem -s swap -r | head -n10 PID User Command Swap USS PSS RSS 18943 hey /usr/lib/firefox/firefox -c 385536 161836 165339 213496 18621 hey /usr/lib/firefox/firefox 225660 458788 479474 576556 1748 hey /opt/discord/Discord --type 84652 130220 137640 153064 1658 hey /opt/discord/Discord 76108 28336 34916 51324 18872 hey /usr/lib/firefox/firefox -c 65172 133824 135810 169480 33599 hey /usr/lib/firefox/firefox -c 55012 261264 266269 324880 20578 hey megasync 52252 13272 15308 24040 35605 hey /usr/lib/firefox/firefox -c 46584 504264 507708 554040 1705 hey /opt/discord/Discord --type 41236 13876 18735 29988

The -r option will reverse the output, which will print the list in descending order.

Читайте также:  Linux создать файл образа

4. Conclusion

In this brief tutorial, we saw how we could view the swap memory used by processes in Linux. We accomplished that by viewing the processes’ information inside the proc directory and using the smem tool.

Источник

How to Check Swap Space in Linux

Wondering how much swap your system has and how much swap is in use? Here’s how to check swap usage in Linux.

You can check swap the same way you check memory usage in Linux: using the free command.

If you use the free command with human readable output (with option -h), it will show you the RAM and Swap uses.

For my system, it shows the following output:

 total used free shared buff/cache available Mem: 7.5G 5.8G 365M 726M 1.4G 787M Swap: 14G 1.1G 13G

As you can see in the above output, my system has 14 GB of total swap space and out of that 1.1 GB is being used. Around 13 GB of swap space is free.

There are more ways to check swap usage in Linux and you may get some additional information about swap as well.

Other ways to check swap usage in Linux

Check Swap Linux

My favorite way to check swap usage in Linux is by using the swapon command. This is a command dedicated for handling swap memory. Apart from giving accurate information about the swap space being used, it also tells you if the swap space is a partition or a swap file.

As you can see in the output, the total swap space is 14.9 GB (not 14 GB as free command showed). 1.1 GB is being used and it’s a swap partition, not a swap file.

NAME TYPE SIZE USED PRIO /dev/nvme0n1p4 partition 14.9G 1.1G -2

Using swap file is a better idea because you easily increase swap size on Linux with it. Changing swap partition is not a good idea.

Читайте также:  Best linux pdf editors

Another way to get swap memory details is by using the /proc/meminfo file. The proc directory in Linux filesystem hierarchy stores system usage information for running processes.

cat /proc/meminfo | grep -i swap
SwapCached: 164116 kB SwapTotal: 15625212 kB SwapFree: 13714756 kB

You can also check swap usage in Linux with top or htop or any other system monitoring tool.

Bonus Tip: Find out which process is using swap space in Linux

You can use an open source utility smem to get more accurate memory usage in Linux. smem is not installed by default so you will have to install it using your distribution’s package manager.

In Ubuntu, smem is available in the universe repository. You can install it using this command:

Once installed, you can use smem to check which process is using how much swap on your system.

You can simply use smem command in the terminal but if your focus is to check swap usage, I suggest reverse sorting it on swap in the following manner:

It will show all the running process in reverse order of swap utilization:

 PID User Command Swap USS PSS RSS 3931 abhishek /opt/google/chrome/chrome 147668 296852 300926 341716 7483 abhishek /opt/google/chrome/chrome - 141524 392900 398545 464700 15774 abhishek /opt/google/chrome/chrome - 127256 454080 456262 525048 1400 abhishek Telegram -- 122060 20528 22514 24832 3964 abhishek /opt/google/chrome/chrome - 108436 101632 111780 138312 2774 abhishek /usr/bin/gnome-shell 101936 204676 210229 225780 9170 abhishek /opt/google/chrome/chrome - 71620 68184 70255 121364 473 abhishek /opt/google/chrome/chrome - 57768 265880 267665 334252 8733 abhishek /opt/google/chrome/chrome - 52236 47280 48685 104332 9422 abhishek /opt/google/chrome/chrome - 41520 62708 64566 120084 

The memory utilization is in kb.

Well, I hope you now know how to check swap memory usage in Linux. You may also want to learn about clearing swap on Linux.

If you are using swap file, you can learn to increase swap size in this video on our YouTube channel:

If you have questions or suggestions, please let me know in the comments below.

Источник

Оцените статью
Adblock
detector