Linux who use swap

How can I know which process is using swap?

There is lots of memory avaiable(about 4G) but swap is used(200+M) in my fedora box. I wonder which process is using swap. How can I know it. ps and top only show the memory usage. Thanks in advance.

4 Answers 4

Improving the cyberciti.biz command to show a more concise answer:

(echo "COMM PID SWAP"; for file in /proc/*/status ; do awk '/^Pid|VmSwap|Name/END< print "">' $file; done | grep kB | grep -wv "0 kB" | sort -k 3 -n -r) | column -t 
COMM PID SWAP dockerd 662 2736 kB skypeforlinux 26865 1320 kB NetworkManager 303 1112 kB slim 392 1028 kB redis-server 350 204 kB 

[a] /proc/meminfo — This file reports statistics about memory usage on the system. It is used by free to report the amount of free and used memory (both physical and swap) on the system as well as the shared memory and buffers used by the kernel. You can also use free, vmstat and other tools to find out the same information.

[b] /proc/$/smaps, /proc/$/status, and /proc/$/stat : Use these files to find information about memory, pages and swap used by each process using its PID.

[c] smem — This command (python script) reports memory usage with shared memory divided proportionally.

Also you can refer Find out what is using your swap

#!/bin/bash # Get current swap usage for all running processes # Erik Ljungstrom 27/05/2011 SUM=0 OVERALL=0 for DIR in `find /proc/ -maxdepth 1 -type d | egrep "^/proc/6"` ; do PID=`echo $DIR | cut -d / -f 3` PROGNAME=`ps -p $PID -o comm --no-headers` for SWAP in `grep Swap $DIR/smaps 2>/dev/null| awk '< print $2 >'` do let SUM=$SUM+$SWAP done echo "PID=$PID - Swap used: $SUM - ($PROGNAME )" let OVERALL=$OVERALL+$SUM SUM=0 done echo "Overall swap used: $OVERALL" 

Источник

How to find out which processes are using swap space in Linux?

Swap space is common in Linux, and it usually is used when the amount of the physical memory(RAM) is full. The idea behind the swap space is that if the operating system needs more memory resources and the RAM doesn’t have any space left, then the pages that are inactive will be moved to the swap space.

It should also be noted that while swap space definitely helps out the RAM on a short basis, they should not be considered a replacement for more RAM.

Now we know a bit about swap spaces, let’s talk about how we can detect which processes are using swap space in Linux.

There are many ways to know about the processes that are making use of swap space, the most basic approach is to use the top command and then press f,, scroll down to where it says SWAP press space followed by typing q.

Читайте также:  Установка deb файлов на линукс

Though it is recommended to either make use of shell script or use Linux utilities like smem.

Using SMEM

SMEM on linux is a command-line utility that is used to provide diverse reports on memory usage on a lInux system.

Installing SMEM

In order to install smem on your local machine, follow the commands shown below −

For Ubuntu/Fedora

Syntax

When we run the command shown below, we will get different processes with their PIDs, Usernames, Commands and their swap memory usage.

Command

Output

PID User Command Swap USS PSS RSS 46740 xxxxxxxx /usr/bin/php-cgi 2904 0 2 4 3623 root ssh-agent-1 572 4 4 4 53398 xxxxxxx /usr/bin/php-cgi 2748 4 4 8 53396 immukul /usr/bin/go-cgi 2788 4 4 8 7855 root rpc.rquotad 124 4 6 116 7380 root ssh-agent-1 6e4 4 3 112 34802 root ssh-agent-1 576 4 8 9

We can clearly notice that the process mentioned in the above output are making use of the swap space.

Another approach to get the processes that are using the swap space is to write a shell script.

Consider the script shown below which will print if any process of the directory that you provide is using a swap space.

Script

SUM=0 OVERALL=0 for DIR in `find /usr/local/ -maxdepth 1 -type d -regex "^/proc/3+"` do PID=`echo $DIR | cut -d / -f 3` PROGNAME=`ps -p $PID -o comm --no-headers` for SWAP in `grep VmSwap $DIR/status 2 > /dev/null | awk '< print $2 >'` do let SUM=$SUM + $SWAP done if (( $SUM > 0 )); then echo "PID=$PID swapped $SUM KB ($PROGNAME)" fi let OVERALL=$OVERALL + $SUM SUM=0 done echo "Overall swap used on machine : $OVERALL KB"

Output

Overall swap used on machine : 26067 KB

Источник

28 Июл 2020 18:07:11 | 2 комментария

Как быстро выяснить какой процесс в Linux использует пространство подкачки (swap)

Заметка очень короткая и призвана администраторам помочь быстро найти процессы которые максимально используют пространство swap. Что делать с этими процессами — это уже отдельная тема, главное найти кто потребляет swap.

Исходные данные: ОС Oracle Linux 7;
Задач: Найти потребителя SWAP

Типичная ситуация на сервере с системой мониторинга — это аларм вида:
prod-srv-01 Low free swap space (free: 0.15 %, threshold: 10%, alert started: 8.79 %)

Вначале немного теории, о том как получить информацию о распределении памяти процессами в Linux.

Мы видим, что swap заполнен на 100% — это плохо. Попробуем быстро выяснить кто основной потребитель, для этого обратимся к /proc/*/status
Ниже простой сценарий на bash который выдаст нам список потребителей swap:

for file in /proc/*/status ; do awk '/VmSwap|Name/END< print "">' $file; done | sort -k 2 -n -r | less

Мы видим, что основной потребитель — это процесс ora_j001_bs. На сервере установлен Oracle и один из процессов потребляет swap.
На втором месте мы видим процесс rsyslogd — думаю он в представлении не нуждается. Если на потребителя №1 мы не можем повлиять быстро, то на потребителя №2 (rsyslogd) можем — это попытаться его перазапустить. Выполняем перезапуск rsyslogd:

systemctl restart rsyslog
[[email protected] ~]# free -h total used free shared buff/cache available Mem: 15G 2.8G 301M 5.6G 12G 6.9G Swap: 5.0G 3.4G 1.6G

Мы видим, что стало доступно 1.6 GB, а это уже более 30% от размера swap, что вполне нас должно устроить на первое время. На этом все, до скорых встреч. Если у Вас возникли вопросы или Вы хотите чтобы я помог Вам, то Вы всегда можете связаться со мной разными доступными способами.

Читайте также:  Sorting file in linux

Источник

How to display processes using swap space

Identify and print processes using swap space to get a better understanding of the Linux operating system.

Display processes using swap space

Use the following command to simply display processes using swap space. This list will be sorted by process id by default due to a way find command returns its results, which are parsed by awk utility.

$ find /proc -maxdepth 2 -path "/proc/9*/status" -readable -exec awk -v FS=":" ' END ' '<>' \;
[..] 11224 bash 520 kB 11372 chrome 4124 kB 11997 python2 1376 kB 14831 chrome 4296 kB 20457 chrome 4580 kB 20463 cat 92 kB 20464 cat 92 kB 20467 chrome 5204 kB 20468 nacl_helper 420 kB 20471 chrome 5100 kB 20587 chrome 12212 kB 20629 chrome 8224 kB [..]

Display processes using swap space sorted by used space

Use additional awk instance to add a temporary column at the beginning, so data could be easily sorted by used swap space in ascending order.

$ find /proc -maxdepth 2 -path "/proc/5*/status" -readable -exec awk -v FS=":" ' END ' '<>' \; | awk '' | sort -h | cut -d " " -f2-
[..] 20587 chrome 12212 kB 2098 firefox 12588 kB 2080 applet.py 13072 kB 10801 Web Content 15796 kB 21412 atom 17384 kB 1629 cinnamon 18584 kB 1300 Xorg 22048 kB 28740 atom 22692 kB 21482 atom 32800 kB 28761 atom 51644 kB 21444 atom 68044 kB 21432 atom 77080 kB

Display top ten processes using swap space

The following command will sort processes by used swap space in descending order, then execute head utility to limit the number of records.

$ find /proc -maxdepth 2 -path "/proc/9*/status" -readable -exec awk -v FS=":" ' END ' '<>' \; | awk '' | sort -hr | head | cut -d " " -f2-
21432 atom 77080 kB 21444 atom 67596 kB 28761 atom 51644 kB 21482 atom 32800 kB 28740 atom 22692 kB 1300 Xorg 22048 kB 1629 cinnamon 18584 kB 21412 atom 17384 kB 10801 Web Content 15796 kB 2080 applet.py 13072 kB

Display top ten processes using swap space with percentage values

Read (see 1st command) or calculate (see 2nd command) total available swap space to calculate and display per-process percentage swap usage. Both of these commands are equivalent.

$ find /proc -maxdepth 2 -path "/proc/8*/status" -readable -exec awk -v FS=":" -v TOTSWP="$(cat /proc/meminfo | sed -n -e "s/^SwapTotal:[ ]*\(1*\) kB/\1/p")" ' END >' '<>' \; | awk '' | sort -hr | head | cut -d " " -f2-
$ find /proc -maxdepth 2 -path "/proc/5*/status" -readable -exec awk -v FS=":" -v TOTSWP="$(cat /proc/swaps | sed 1d | awk 'BEGIN  END')" ' END >' '<>' \; | awk '' | sort -hr | head | cut -d " " -f2-
21432 atom 77080 kB 0.93% 21444 atom 67596 kB 0.82% 28761 atom 51644 kB 0.62% 21482 atom 32800 kB 0.40% 28740 atom 22692 kB 0.27% 1300 Xorg 22048 kB 0.27% 1629 cinnamon 18584 kB 0.22% 21412 atom 17384 kB 0.21% 10801 Web Content 15796 kB 0.19% 2080 applet.py 13072 kB 0.16%

You can extend this idea further by creating bar charts in terminal. awk is beautiful!

Follow me on Mastodon , check out source code ad GitHub

Источник

Best way to tell what application/process is using swap?

enter image description here

I have 16G RAM, and 8G swap partition. I’m running Unity 17.04. I have a problem where my buff/cache goes to 11M, and my swap used goes to around 3500. Previously I almost never used any swap space, and I’ve never monitored the buff/cache. I believe that it’s an application with a memory leak, but I could be wrong. Simple question. Is there any easy way to determine with application/process is, or has been, using swap?

One other possibility is that vm.swappiness might be set high, which can push things into swap if the OS thinks it won’t need them for a long time, even if there’s plenty of open RAM.

@ChaiT.Rex hum. I wonder. pre 17.04 I had vm.swappiness=10. but after I upgraded to 17.04 I set it back to the default of 60. I wonder if that’s about the time that I noticed this? I may have to set it back and see what happens. Thanks!

@ChaiT.Rex well, I played with vm.swappiness, and finally settled on =10 again, like it was before. My buff/cache usage is still 11-12G after a few days, but I guess that’s normal, as long as the avail Mem stays high. If you make your comment into an answer, I can vote/accept it.

1 Answer 1

You can find that how much does special process uses swap partition by this command :

cat /proc/"PID"/status | grep "^VmSwap" 

And you can find PID by this command:

ps -A | grep "Application_name" 

But if you want to find which processes are using the swap partition, you can use this script:

#!/bin/bash for i in /proc/*/status ; do vmswap=$(cat $i | grep "^VmSwap") echo "$vmswap" | grep -qv ' 0 kB' if [ $? == 0 ] && [ "$vmswap" != "" ] ; then echo "$i : $vmswap" fi done 

Then you can find the application name from its PID that’s returned by the script.

Update: I changed this script to create log file every 10 second (you can change the time) and in that file you can see many processes from the moment you run this script up to now:

#!/bin/bash counter=1 touch ~/swap_process_usage.log while true ; do echo -e "************************************\nSwap's process in count $counter " >> ~/swap_process_usage.log for i in /proc/*/status ; do vmswap=$(cat $i | grep "^VmSwap") echo "$vmswap" | grep -qv ' 0 kB' if [ $? == 0 ] && [ "$vmswap" != "" ] ; then pid=$(echo "$i" | tr -d /proc/ | tr -d status) proc_name=$(ps -p $pid -o comm=) echo "$proc_name : $pid : $vmswap" >> ~/swap_process_usage.log fi done sleep 10s counter=$((counter+1)) done 

And you can set this script to run at startup so it creates log every time.

Источник

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