View server Swap Usage

Source: Internet
Author: User

Recent memory usage shows a large swap consumption, but there is no command to see directly the size of a process occupying swap. You can only see the total swap and how much.

In fact, in the proc under the Process Number command folder is the use of swap, only need to add all the size of the beginning of the swap to get the total size.

The following is an entry occupied by a process:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/72/FC/wKiom1XxNr-DVGYVAACyOFfu_2c570.jpg "title=" 1.jpg " alt= "Wkiom1xxnr-dvgyvaacyoffu_2c570.jpg"/>


Can write a simple script to achieve statistical functions:

#!/bin/bash

###############################################################################

# Date: 2015-09-10

# Author: Wangtenghe

# email: [Email protected]

# Version: 2.0

# Scripting Feature: Lists the processes that are occupying swap.

###############################################################################

Echo-e "Pid\t\tswap\t\tproc_name"

For PID in ' Ls-l/proc | grep ^d | awk ' {print $9} ' | grep-v [^0-9] '

    1. # all directories in the/proc directory (the process name is the number is the process, others such as sys,net, etc. are stored in other information)

Do

If [$pid-eq 1];then continue;fi # do not check init process

    1. # There is only one way for the process to release swap: Restart the process. Or wait for it to be released automatically.

    2. # If the process is automatically released, then we will not write a script to find him, looking for him because he did not automatically release.

    3. # So we're going to list the processes that occupy swap and need to be restarted, but the Init process is the ancestor of all the processes in the system

    4. # Restarting the init process means focusing on the system, which is absolutely not possible, so you do not have to detect him, so as not to affect the system.

Grep-q "Swap"/proc/$pid/smaps 2>/dev/null #检查是否占用swap分区

If [$?-eq 0];then

swap=$ (gawk '/swap/{sum+=$2;} end{print sum} '/proc/$pid/smaps) #统计占用的swap分区的大小 Unit is KB

proc_name=$ (ps aux | grep-w "$pid" | awk '!/grep/{for (i=11;i<=nf;i++) {printf ("%s", $i);}} ') #取出进程的名字

If [$swap-gt 0];then #判断是否占用swap Only the consumption will be output

Echo-e "$pid \t${swap}\t$proc_name"

Fi

Fi

Done | Sort-k2-n | Gawk-f ' \ t ' {


if ($2<1024)

printf ("%-10s\t%15skb\t%s\n", $1,$2,$3);

else if ($2<1048576)

printf ("%-10s\t%15.2fmb\t%s\n", $1,$2/1024,$3);

Else

printf ("%-10s\t%15.2fgb\t%s\n", $1,$2/1048576,$3);

}‘

#按占用swap的大小排序, and then use awk for unit conversions.

    1. # For example: Convert 1024KB to 1 m. Convert 1048576KB to 1G to improve readability.


Execution Result:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/72/FC/wKiom1XxOvnwBlOsAAHNLM81xCw586.jpg "title=" 1.jpg " alt= "Wkiom1xxovnwblosaahnlm81xcw586.jpg"/>

If a program consumes a large amount of swap, restart the process. Otherwise, it is likely that the system will crash due to the depletion of the swap.


Reference: http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=30359985&id=5189506


View server Swap Usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.