How to check swap linux

how to check what is in swap?

How to check what is in swap? I try to check it via processes, but for every pid on system VmSwap is 0:

undefine@uml:~$ awk '/VmSwap/ ' /proc/*/status |uniq 0 

What else can be in swap? I thought about tmpfs — but i reread all files on tmpfs-es — and it doesn’t flush swap size.

2 Answers 2

smem is the standard tool for this. It’s clean and simple.

On a Debian based system, install it via package manager:

A sample (clipped) output from my system:

$ smem -s swap -t -k -n PID User Command Swap USS PSS RSS 831 1000 /bin/bash 0 3.8M 3.8M 5.5M 3931 1000 bash /usr/bin/sage -c noteb 276.0K 4.0K 20.0K 1.2M 17201 1000 /usr/bin/dbus-launch --exit 284.0K 4.0K 8.0K 500.0K 17282 1000 /usr/bin/mate-settings-daem 372.0K 11.0M 11.7M 21.8M 17284 1000 marco 432.0K 16.7M 18.1M 29.5M 17053 1000 mate-session 952.0K 3.3M 3.5M 9.2M 3972 1000 python /usr/lib/sagemath/sr 2.7M 101.8M 102.1M 104.3M ------------------------------------------------------------------------------- 141 1 5.2M 3.9G 4.0G 4.5G 

But does it return anything other than the value shown in the VmSwap line in /proc/PID/status ? The question isn’t asking for a prettier display, it’s asking what could be in swap other than process data.

smem doesn’t give me nothing more usefull than greping proc: undefine@uml:~$ sudo smem -c swap |uniq -c 1 Swap 227 0

I only started looking into it yesterday for my needs, below is what I’ve found so far:

SWAP_USED = Used_by_Processes + SwapCached + Part_of_Tmpfs + something_else

Short story:

Used_by_Processes – data that has been swapped out from memory completely.

SwapCached – data that has been swapped to disk, but still remains in memory.

Part_of_Tmpfs – some portion of tmpfs data.

Long story:

Used_by_Processes – there are many instructions published on how to mis-calculate this one 😉 E.g. if we sum up all VmSwap entries from /proc/*/status or Swap entries from /proc/*/smaps — we will get an overestimate (shared swapped pages could get counted more than once). If we don’t run it from root user or our OS — the underestimate will be silently returned. I don’t have a proper way of identifying shared pages, but splashing of same ‘maps’ gives much better approximation than other approaches: (note that cat below is not useless and actually needs a 2>/dev/null )

[root@a:~]# cat /proc/*/status|awk ' /^VmSwap/< s+=$2 >END' 32048 [root@a:~]# cat /proc/*/smaps|awk ' /^Swap/< s+=$2 >END' 32048 [root@a:~]# cat /proc/*/smaps|awk '/-//^Swap/END' 14940 [root@a:~]# free -k|grep -e Swap -e used total used free shared buffers cached Swap: 8388600 15508 8373092 

SwapCached – this one is straightforward and can be cleanly extracted from /proc/meminfo . Some people wouldn’t expect this to be counted as «used» swap, since a duplicate (non-dirty) copy of same page in both RAM and Swap can be freed on either side quite instantly (in case if demand comes) thus making one of the copies «freed».

Читайте также:  Нагрузка на видеокарту linux

Part_of_Tmpfs – the bright side is that when all Your tmpfs data is many-days-untouched and swappiness is non-zero — it’s quite likely that entire tmpfs is swapped-out (and vice-versa for recently-used data). The downside is I’ve found no API to reliably calculate the threshold or percentage of how much of it IS swapped, though if there’s enough RAM — we can copy entire tmpfs data into /dev/null and thus get some clue of how much of it WAS swapped.

Common mistakes made during calculation of tmpfs size are — assuming that /dev/shm is the only tmpfs configured or trying to do it by recursive per-file scanning (not only implementations tend to omit hidden files or do it from non- root , but it also un-swaps some pages during traversing). Much easier way is to use good old df .

something_else – see the » diff 385 MB » below, needs a dive into kernel sources. See my script:

#!/bin/bash TMPFS=`df -kP |awk ' /^tmpfs/< s+=$3 >END'` PROCS=`cat /proc/*/smaps|awk '/-/ /^Swap/END'` SCACH=`cat /proc/meminfo|awk ' /^SwapCached/ '` TOTAL=`free -k |awk ' /^Swap/ '` echo -e " df $TMPFS\t smaps $PROCS \tSwapCache $SCACH\t| $TOTAL\tswap | diff $[TOTAL-TMPFS-PROCS-SCACH]\tMB" 

and the output from different boxes:

xa002: df 0 smaps 271 SwapCache 3858 | 4120 swap | diff -9 MB sg003: df 0 smaps 234 SwapCache 3876 | 4111 swap | diff 1 MB sg001: df 0 smaps 245 SwapCache 3845 | 4093 swap | diff 3 MB sg002: df 0 smaps 244 SwapCache 3843 | 4091 swap | diff 4 MB dm001: df 2 smaps 971 SwapCache 728 | 1707 swap | diff 6 MB hm012: df 270 smaps 161 SwapCache 29 | 454 swap | diff -6 MB hm003: df 274 smaps 142 SwapCache 27 | 440 swap | diff -3 MB hm006: df 262 smaps 150 SwapCache 29 | 437 swap | diff -4 MB hm002: df 265 smaps 120 SwapCache 28 | 412 swap | diff -1 MB hm009: df 258 smaps 124 SwapCache 33 | 410 swap | diff -5 MB hm011: df 262 smaps 118 SwapCache 28 | 406 swap | diff -2 MB hm008: df 245 smaps 122 SwapCache 32 | 396 swap | diff -3 MB hm005: df 247 smaps 120 SwapCache 33 | 396 swap | diff -4 MB dp001: df 0 smaps 0 SwapCache 0 | 386 swap | diff 386 MB hm014: df 184 smaps 134 SwapCache 34 | 343 swap | diff -9 MB hm007: df 0 smaps 132 SwapCache 32 | 158 swap | diff -6 MB bm002: df 0 smaps 121 SwapCache 25 | 141 swap | diff -5 MB dm002: df 2 smaps 70 SwapCache 71 | 139 swap | diff -4 MB bm001: df 3 smaps 102 SwapCache 28 | 131 swap | diff -2 MB bm004: df 0 smaps 98 SwapCache 29 | 126 swap | diff -1 MB hm013: df 0 smaps 100 SwapCache 30 | 124 swap | diff -6 MB bm006: df 0 smaps 103 SwapCache 15 | 122 swap | diff 4 MB hm010: df 0 smaps 102 SwapCache 24 | 122 swap | diff -4 MB hm001: df 0 smaps 101 SwapCache 25 | 121 swap | diff -5 MB bm003: df 0 smaps 98 SwapCache 15 | 107 swap | diff -6 MB bm005: df 0 smaps 70 SwapCache 17 | 85 swap | diff -2 MB sg004: df 0 smaps 72 SwapCache 14 | 83 swap | diff -3 MB sg001: df 0 smaps 41 SwapCache 33 | 78 swap | diff 4 MB sg005: df 0 smaps 59 SwapCache 20 | 75 swap | diff -4 MB sg003: df 0 smaps 58 SwapCache 18 | 72 swap | diff -4 MB sg006: df 0 smaps 56 SwapCache 13 | 65 swap | diff -4 MB sg002: df 0 smaps 54 SwapCache 12 | 64 swap | diff -2 MB xa001: df 0 smaps 56 SwapCache 5 | 55 swap | diff -6 MB 

And a small experiment as the bonus:

[root@hm012:~]# df -h|grep -e '^Filesystem' -e '^tmpfs' Filesystem Size Used Avail Use% Mounted on tmpfs 12G 271M 12G 3% /dev/shm tmpfs 8.0G 84K 8.0G 1% /tmp [root@hm012:~]# ./get_swap.sh df 270 smaps 161 SwapCache 29 | 454 swap | diff -6 MB [root@hm012:~]# rm -rf /dev/shm/* [root@hm012:~]# df -h|grep -e '^Filesystem' -e '^tmpfs' Filesystem Size Used Avail Use% Mounted on tmpfs 12G 0 12G 0% /dev/shm tmpfs 8.0G 84K 8.0G 1% /tmp [root@hm012:~]# ./get_swap.sh df 0 smaps 161 SwapCache 29 | 185 swap | diff -5 MB 

P.S. aside from the approximation mentioned above — there are other sources of error, like rounding of KB into MB, theoretical possibility of a mismatch between block-sizes of RAM and Swap, etc. I’m not sure it covers everything, but hoping this helps to some extent 🙂

Читайте также:  Драйвер canon 6000 драйвер линукс

Источник

How to Check Swap Memory in Linux: A Comprehensive Guide

Learn how to check swap memory utilization and usage in Linux using various commands, including free, swapon, /proc/swaps, top, vmstat, and sar.

  • Using the free command
  • Using other commands to monitor swap space usage
  • Linux Crash Course — Understanding Memory and Swap Usage
  • Checking swap space on the system
  • Checking swap usage for each process
  • Clearing swap space
  • Other quick code samples for checking Linux swap memory usage
  • Conclusion
  • How to check swap memory in Linux using top?
  • Where is swap memory in Linux?
  • What is the swap memory in Linux?
  • How much memory do I have for swap?

As a Linux user, you might have heard about swap memory. Swap memory is an essential part of Linux systems that helps manage memory usage when physical memory (RAM) is full. In this article, we will discuss various ways to check swap memory in Linux.

Using the free command

The free command is a popular utility that can be used to check memory and swap utilization on the system. It is available on most Linux distributions by default. To check swap memory using the free command, open a terminal window and type the following command:

The -m option is used to display the output in megabytes, making it easier to read. The output of the free command shows the total, used, and free swap memory.

 total used free shared buff/cache available Mem: 985 324 258 36 402 422 Swap: 1023 0 1023 

In the above output, we can see that the total swap memory is 1023 megabytes, and there is no swap memory currently being used.

Using other commands to monitor swap space usage

Apart from the free command, there are several other commands that can be used to monitor swap space usage.

The swapon command

The swapon command can be used to enable or disable swap space on the system. To enable swap space, use the following command:

$ sudo swapon /path/to/swapfile 

Replace /path/to/swapfile with the path to the swap file on your system. To disable swap space, use the following command:

$ sudo swapoff /path/to/swapfile 

The /proc/swaps file

The /proc/swaps file displays information about the swap space usage. To view the contents of the /proc/swaps file, type the following command:

Читайте также:  Gcc компилятор linux установка

The output will show the path of the swap file or partition along with the size, used, and priority.

Filename Type Size Used Priority /dev/sda2 partition 2097148 0 -2 

The top command

The top command shows the current swap space usage by each process. To launch the top command, type the following command:

Once the top command is launched, press the f key to select the fields to display. Use the arrow keys to move down to the SWAP field and press the d key to enable it. The top command will now display the swap space usage by each process.

The vmstat and sar commands

The vmstat and sar commands provide detailed information about the memory and swap usage. To use the vmstat command, type the following command:

The output will show the total swap space size and the amount of swap space used.

 2097148 K total swap space 0 K used swap space 2097148 K free swap space 1048576 K total high memory 350252 K used high memory 698324 K free high memory 

To use the sar command, type the following command:

The output will show the swap space usage for each minute.

Linux Crash Course — Understanding Memory and Swap Usage

Checking swap space on the system

To view the swap space on the system, use the swapon —show command. This command shows the path of the swap file or partition along with the size, used, and priority.

$ swapon --show NAME TYPE SIZE USED PRIO /dev/sda2 partition 2G 0B -2 

The location of the swap space can also be checked using the cat /proc/swaps or swapon -s command.

Checking swap usage for each process

The Swap column in /proc/[PID]/smaps shows the amount of swap space used by each process. To check the size and current usage of swap space, use the following command:

$ grep SwapTotal /proc/meminfo 

The output will show the total swap space size and the amount of swap space used.

SwapTotal: 2097148 kB SwapFree: 2097148 kB SwapCached: 0 kB 

Clearing swap space

Swap space can be cleared using the swapoff command. The command disables all swap space on the system.

Other quick code samples for checking Linux swap memory usage

In Shell , for instance, how to free swap memory in linux code sample

sudo swapoff -a; sudo swapon -a

In Shell , for example, how to check swap memory in linux code sample

Conclusion

Swap memory is an important aspect of Linux systems that helps manage memory usage when RAM is full. Various commands can be used to check swap memory utilization and usage in Linux. It is essential to monitor swap space usage and ensure that it does not affect system performance. Best practices suggest setting up appropriate swap space during the operating system installation and avoiding disabling it unless necessary. With the knowledge gained from this article, you can now effectively monitor and manage swap memory on your Linux system.

Источник

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