
1.free
free -m
就能看出当前系统所使用的swap了。那么如何查看哪些进程使用了swap呢,这样好针对性的做出优化。
2.top
Centos(6.0之前):
top只能看到swap总使用量
网上很多人说top+f+p能显示出来swap。可是按完f查看的时候,man top里面swap的解释是:
并不是实际的使用swap。而是VIRT-RES得来的。用我蹩脚的英文翻译就是,虚拟内存中所使用过的swap部分
3.Centos(6.0之后):
man top
这样就明显看出是取出的每个进程的swap,能很方便的查看哪些进程使用了swap。从中也能看到一个信息。那就是读取了/proc/#/status
4.vmstat
vmstat -n 1也能查看到
仍旧无法查看到哪些进程使用了。但是能看到si、so
Memory(内存):
swpd: 使用虚拟内存大小
free: 可用内存大小
buff: 用作缓冲的内存大小
cache: 用作缓存的内存大小
Swap:
si: 每秒从交换区写到内存的大小
so: 每秒写入交换区的内存大小
5.shell
在Linux内核 2.6.16中引入了一个系统内存接口特性,这个接口位于/proc/$pid/目录下的smaps文件中 ,一看内容发现是进程内存映像信息,比同一目录下的maps文件更详细些。
cat /proc/1/smaps
这里解释下samps里面的内容:
bfdca000-bfddf000 是该虚拟内存段的开始和结束位置
rw-p 内存段的权限,rw是指可读写,p是指私有,如果是s则为共享
bffea000 该虚拟内存段在对应的映射文件中的偏移量
00:00 文件的主设备和次设备号
0 被映射到虚拟内存的文件的索引节点号
[stack] 被映射到虚拟内存的文件名称
Size 是进程使用内存空间,并不一定实际分配了内存(VSS)
Rss是实际分配的内存(不需要缺页中断就可以使用的)
Shared_Clean 和其他进程共享的未改写页面
Shared_Dirty 和其他进程共享的已改写页面
Private_Clean 未改写的私有页面页面
Private_Dirty 已改写的私有页面页面
Swap 存在于交换分区的数据大小(如果物理内存有限,可能存在一部分在主存一部分在交换分区)
Pss是平摊计算后的使用内存(有些内存会和其他进程共享,例如mmap进来的)
08048000-080bc000 地址空间的开始地址 - 结束地址
r-xp 属性。前三个是rwx(读、写、可执行),如果不具有则为“-”。最后一个是p/s(私有/共享)
00000000 偏移量。如果这段内存是从文件里映射过来的,则偏移量为这段内容在文件中的偏移量。如果不是从文件里面映射过来的则为0.
03:02 If the region was mapped from a file, this is the major and minor device number (in hex) where the file lives.
13130 If the region was mapped from a file, this is the file number.
/bin/bash If the region was mapped from a file, this is the name of the file. This field is blank for anonymous mapped regions. There are also special regions with names like [heap], [stack], or [vdso]. [vdso] stands for virtual dynamic shared object. It’s used by system calls to switch to kernel mode.
Rss-Resident Set Size 实际使用物理内存(包含共享库占用的内存)
Rss=Shared_Clean+Shared_Dirty+Private_Clean+Private_Dirty
Pss 实际使用的物理内存(按比例包含共享库占用的内存)。比如四个进程共享同一个占内存1000MB的共享库,每个进程算进250MB在Pss。
Shared_Clean 、 Shared_Dirty 、 Private_Clean、 Private_Dirty
(shared/private)共享和私有
一个页的clean字段表示没有更改此页,当发生换页时不用写回。dirty表示更改了此页,当发生换页时要写回磁盘。此处这四个值是遍历页表中各个页后得到的。
“Referenced” indicates the amount of memory currently marked as referenced or accessed. “Anonymous” shows the amount of memory that does not belong to any file. Even a mapping associated with a file may contain anonymous pages: when MAP_PRIVATE and a page is modified, the file page is replaced by a private anonymous copy. “Swap” shows how much would-be-anonymous memory is also used, but out on swap.
linux内存使用情况查看命令查看内存:freewww.dnjsb.com
total
used
free
shared
buffers
cachedMem:
8126976
7609376
517600
0
447392
1268632-/+
buffers/cache:
5893352
2233624Swap:
2928636
572388
2356248top查看进程,shift+f可以设置排序顺序,如果按照RES排序,可以保证清晰的查看到系统占用内存较大的线程。
www.dnjsb.com
ps:查看进程情况,
一般使用ps
-ef,
ps
-aux-A
:所有的
process
均显示出来,与
-e
具有同样的效用;-a
:不与
terminal
有关的所有
process
;-u
:有效使用者
(effective
user)
相关的
process
;x
:通常与
a
这个参数一起使用,可列出较完整信息。输出格式规划:l
:较长、较详细的将该
PID
的的信息列出;j
:工作的格式
(jobs
format)-f
:做一个更为完整的输出。红色为父进程idguest
12032
11991
0
21:32
pts/0
00:00:00
grep
httpdadmin
14969
26047
0
09:21
?
00:00:01
/usr/alibaba/httpd/bin/httpd
-d
/home/admin/run/deployadmin
20417
26047
0
09:36
?
00:00:01
/usr/alibaba/httpd/bin/httpd
-d
/home/admin/run/deployroot
26047
1
0
Nov19
?
00:00:00
/usr/alibaba/httpd/bin/httpd
-d
/home/admin/run/deploy通过pstree
-p
26047,
可以看到所有的子进程的线程!查看swap使用情况:#!/bin/bashPID=${1:-[1-9]*}total_swap=0GetSwap
()
{
pid=$1
cmdline=$(cat
/proc/$pid/cmdline
2>/dev/null|tr
''
'
')
pid_swap=$(awk
'BEGIN{total=0}/Swap/{total+=$2}END{print
total}'
/proc/$pid/smaps
2>/dev/null)
if
[
$pid_swap
!=
''
]
&&
[
$pid_swap
-gt
0
]
then
echo
PID=$pid
–
Swap
used:
$pid_swap
Kb
–
($cmdline)
fi}cd
/procfor
pid
in
$PID
do
GetSwap
$pid
let
total_swap+=$pid_swapdoneecho
Total
swap:
$total_swap
Kb
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)