Linux进程虚拟内存和物理内存
Linux进程虚拟内存和物理内存
先介绍几个基本概念:
SIZE : 进程使用的地址空间, 如果进程映射了100M的内存, 进程的地址空间将报告为100M内存. 事实上, 这个大小不是一个程序实际使用的内存数.
RSS : "Resident Set Size", 实际驻留"在内存中"的内存数. 不包括已经交换出去的代码. 举一个例子: 如果你有一个程序使用了100K内存, 操作系统交换出40K内存, 那么RSS为60K. RSS还包括了与其它进程共享的内存区域. 这些区域通常用于libc库等.
SHARE : RSS中与其它进程共享的内存部分大小.
VMSIZE : 一个进程占用的总的地址空间大小. 它包括了没有映射到内存中的页面.
Private RSS : 映射到内存中的页面, 这些页面仅由进程单独使用. 这也是我们最关心地方: 进程实际占用的内存数.
如何来查看Private RSS呢? /proc接口中每一个进程目录下的smaps提供了private rss信息. smaps是在2.6.16内核版本引进来的.
私有驻留内存数(Private RSS):
查看/proc/$pid/smaps
下面我以本站使用的Linux AS5为例, 查看进程号1293(vmware-guestd, 本站使用的是一台基于vmware的客户虚拟机,)使用的Private RSS数.
00111000 - 00112000 rwxp 00111000 00 : 00 0
Size: 4 kB
Rss: 4 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 4 kB
0050e000-0050f000 rwxp 0050e000 00 : 00 0
Size: 4 kB
Rss: 0 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 0 kB
0051a000-0051b000 r-xp 0051a000 00 : 00 0 [ vdso ]
Size: 4 kB
Rss: 4 kB
Shared_Clean: 4 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 0 kB
0051b000- 00534000 r-xp 00000000 fd: 00 194898 /lib/ld- 2 . 4 . so
Size: 100 kB
Rss: 0 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 0 kB
00534000 - 00535000 r-xp 00018000 fd: 00 194898 /lib/ld- 2 . 4 . so
Size: 4 kB
Rss: 0 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 0 kB
00535000 - 00536000 rwxp 00019000 fd: 00 194898 /lib/ld- 2 . 4 . so
Size: 4 kB
Rss: 0 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 0 kB
00538000 - 00665000 r-xp 00000000 fd: 00 194905 /lib/libc- 2 . 4 . so
Size: 1204 kB
Rss: 212 kB
Shared_Clean: 204 kB
Shared_Dirty: 0 kB
Private_Clean: 8 kB
Private_Dirty: 0 kB
00665000 - 00667000 r-xp 0012d000 fd: 00 194905 /lib/libc- 2 . 4 . so
Size: 8 kB
Rss: 8 kB
Shared_Clean: 4 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 4 kB
00667000 - 00668000 rwxp 0012f000 fd: 00 194905 /lib/libc- 2 . 4 . so
Size: 4 kB
Rss: 4 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 4 kB
00668000 -0066b000 rwxp 00668000 00 : 00 0
Size: 12 kB
Rss: 8 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 8 kB
08047000 - 08062000 r-xp 00000000 fd: 00 292327 /usr/sbin/vmware-guestd
Size: 108 kB
Rss: 64 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 64 kB
Private_Dirty: 0 kB
08062000 - 08063000 rwxp 0001a000 fd: 00 292327 /usr/sbin/vmware-guestd
Size: 4 kB
Rss: 4 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 4 kB
08063000 - 08068000 rwxp 08063000 00 : 00 0
Size: 20 kB
Rss: 12 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 12 kB
08385000 - 08886000 rwxp 08385000 00 : 00 0 [ heap ]
Size: 5124 kB
Rss: 5080 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 5080 kB
bfeb2000-bfec7000 rwxp bfeb2000 00 : 00 0 [ stack ]
Size: 84 kB
Rss: 12 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 0 kB
Private_Dirty: 12 kB
上面我们看到从smaps看不太方便, 推荐使用Ben Maurer写的perl脚本:
#!/usr/bin/perl
# Copyright Ben Maurer
# you can distribute this under the MIT/X11 License
use Linux::Smaps;
my $pid=shift @ARGV;
unless ($pid) {
print "./smem.pl <pid>/n";
exit 1;
}
my $map=Linux::Smaps->new($pid);
my @VMAs = $map->vmas;
format STDOUT =
VMSIZE: @######## kb
$map->size
RSS: @######## kb total
$map->rss
@######## kb shared
$map->shared_clean + $map->shared_dirty
@######## kb private clean
$map->private_clean
@######## kb private dirty
$map->private_dirty
.
write;
printPrivateMappings ();
printSharedMappings ();
sub sharedMappings () {
return grep { ($_->shared_clean + $_->shared_dirty) > 0 } @VMAs;
}
sub privateMappings () {
return grep { ($_->private_clean + $_->private_dirty) > 0 } @VMAs;
}
sub printPrivateMappings ()
{
$TYPE = "PRIVATE MAPPINGS";
$^ = 'SECTION_HEADER';
$~ = 'SECTION_ITEM';
$- = 0;
$= = 100000000;
foreach $vma (sort {-($a->private_dirty <=> $b->private_dirty)}
privateMappings ()) {
$size = $vma->size;
$dirty = $vma->private_dirty;
$clean = $vma->private_clean;
$file = $vma->file_name;
write;
}
}
sub printSharedMappings ()
{
$TYPE = "SHARED MAPPINGS";
$^ = 'SECTION_HEADER';
$~ = 'SECTION_ITEM';
$- = 0;
$= = 100000000;
foreach $vma (sort {-(($a->shared_clean + $a->shared_dirty)
<=>
($b->shared_clean + $b->shared_dirty))}
sharedMappings ()) {
$size = $vma->size;
$dirty = $vma->shared_dirty;
$clean = $vma->shared_clean;
$file = $vma->file_name;
write;
}
}
format SECTION_HEADER =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$TYPE
@>>>>>>>>>> @>>>>>>>>>> @>>>>>>>>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
"vmsize" "rss clean" "rss dirty" "file"
.
format SECTION_ITEM =
@####### kb @####### kb @####### kb @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$size $clean $dirty $file
.
下面是由seme.pl脚本解析的smaps数据:
首先安装Linux::Smaps模块:
perl -MCPAN -e 'install Linux::Smaps'//需要网络,如无法上网,则可下载压缩包解压到 /root
然后用seme.pl解析1293进程的Smaps数据:
VMSIZE: 7200 kb
RSS: 1052 kb total
192 kb shared
100 kb private clean
760 kb private dirty
PRIVATE MAPPINGS
vmsize rss clean rss dirty file
5636 kb 8 kb 724 kb [ heap ]
84 kb 0 kb 12 kb [ stack ]
4 kb 0 kb 4 kb
8 kb 0 kb 4 kb /lib/libc- 2 . 4 . so
4 kb 0 kb 4 kb /lib/libc- 2 . 4 . so
12 kb 4 kb 4 kb
4 kb 0 kb 4 kb /usr/sbin/vmware-guestd
20 kb 8 kb 4 kb
1204 kb 16 kb 0 kb /lib/libc- 2 . 4 . so
108 kb 64 kb 0 kb /usr/sbin/vmware-guestd
SHARED MAPPINGS
vmsize rss clean rss dirty file
1204 kb 188 kb 0 kb /lib/libc- 2 . 4 . so
4 kb 4 kb 0 kb [ vdso ]
从上面看到rss大小被分成了两个部分: private(私有)和shared(共享).
private rss就是我们最关心的进程实际占用的内存数.
http://hi.baidu.com/youngtao/blog/item/70621d83c72933a50cf4d29a.html
更多推荐
所有评论(0)