这是一篇关于Linux内存measuring的基础教程。除了free之外,我们可以通过/proc得到更多信息。它们是/proc下的maps,smaps 和statm。
‘free’ and /proc
‘free’ 会显示机器上的内存使用信息。能实时跟踪整个系统内存的使用情况。
$ free
total used free shared buffers cached
Mem: 507564 481560 26004 0 68888 185220
-/+ buffers/cache: 227452 280112
Swap: 2136604 105168 2031436
除了free命令之外,/proc/meminfo能够提供更多的信息。以下就是一个例子: 512 Mb RAM, 运行在 Linux 2.6.3:
$ cat /proc/meminfo
MemTotal: 507564 kB
MemFree: 26004 kB
Buffers: 68888 kB
Cached: 185220 kB
SwapCached: 29348 kB
Active: 342488 kB
Inactive: 32092 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 507564 kB
LowFree: 26004 kB
SwapTotal: 2136604 kB
SwapFree: 2031436 kB
Dirty: 88 kB
Writeback: 0 kB
Mapped: 165648 kB
Slab: 73212 kB
Committed_AS: 343172 kB
PageTables: 2644 kB
VmallocTotal: 524212 kB
VmallocUsed: 7692 kB
VmallocChunk: 516328 kB
测量用户进程的内存使用:
‘ps’ 关于内存信息解释 http://www.linuxidc.com
‘ps’ 能够提供内存信息情况。比如:
BJATCA01sta1:/proc/3105(8)#ps -p 3129 -o pid,rss,vsz,comm
PID RSS VSZ COMMAND
3129 23100 213664 GAM
· %Mem – 内存使用百分比
· VSZ – Virtual Size
· RSS – Resident Set Size
· SIZE – Equivalent to VSZ
‘top’ 显示信息可参见 ‘man top’:
· %MEM — Memory usage (RES) 进程当前使用的物理内存。
· VIRT — Virtual Image (kb) 进程整个virtual memory空间, VIRT = SWAP + RES
· SWAP — Swapped size (kb) virtual memory交换到disk或者没有加载到物理内存里的空间。
· RES — Resident size (kb) 进程驻留在内存里面的内存大小, RES = CODE + DATA.
· CODE — Code size (kb) 加载到内存里面的可执行文件代码
· DATA — Data+Stack size (kb) 加载到内存里面的数据和栈
· SHR — Shared Mem size (kb) 共享内存大小
· nDRT — Dirty Pages count 页面已经修改但是还没有保存到disk的数目。
更多信息可以使用命令:’man proc’
/proc/<pid>/statm
/proc/<pid>/statm fields: columns are (in pages):
total program size| |
resident set size| |
shared pages| |
text (code) | |
data/stack | |
library | |
dirty pages | |
Here an example: 693 406 586 158 0 535 0
/proc/<pid>/status
/proc/<pid>/status fields:
· Vm Size: 2772 kB
· Vm Lck: 0 kB – ???
· Vm RSS: 1624 kB
· Vm Data: 404 kB
· Vm Stk: 24 kB
· Vm Exe: 608 kB
· Vm Lib: 1440 kB
/proc/<pid>/maps
maps显示所有共享库映射到进程地址空间的详细情况。Example:
$ cat /proc/25042/maps
08048000-080e0000 r-xp 00000000 03:05 196610 /bin/bash
080e0000-080e6000 rw-p 00097000 03:05 196610 /bin/bash
080e6000-08148000 rwxp 00000000 00:00 0
40000000-40016000 r-xp 00000000 03:05 147471 /lib/ld-2.3.3.so
40016000-40017000 rw-p 00015000 03:05 147471 /lib/ld-2.3.3.so
40017000-40018000 rw-p 00000000 00:00 0
40018000-40019000 r–p 00000000 03:05 184090 /usr/share/locale/en_US/LC_IDENTIFICATION
40019000-4001a000 r–p 00000000 03:05 184089 /usr/share/locale/en_US/LC_MEASUREMENT
4001a000-4001b000 r–p 00000000 03:05 184083 /usr/share/locale/en_US/LC_TELEPHONE
4001b000-4001c000 r–p 00000000 03:05 184091 /usr/share/locale/en_US/LC_ADDRESS
4001c000-4001d000 r–p 00000000 03:05 184086 /usr/share/locale/en_US/LC_NAME
4001d000-4001e000 r–p 00000000 03:05 184084 /usr/share/locale/en_US/LC_PAPER
4001e000-4001f000 r–p 00000000 03:05 184088 /usr/share/locale/en_US/LC_MESSAGES/SYS_LC_MESSAGES
4001f000-40020000 r–p 00000000 03:05 184087 /usr/share/locale/en_US/LC_MONETARY
40020000-40026000 r–p 00000000 03:05 183689 /usr/share/locale/ISO-8859-1/LC_COLLATE
40026000-40027000 r–p 00000000 03:05 184082 /usr/share/locale/en_US/LC_TIME
40027000-4002a000 r-xp 00000000 03:05 147459 /lib/libtermcap.so.2.0.8
4002a000-4002b000 rw-p 00002000 03:05 147459 /lib/libtermcap.so.2.0.8
4002b000-4002c000 rw-p 00000000 00:00 0
4002c000-4002e000 r-xp 00000000 03:05 147482 /lib/libdl-2.3.3.so
4002e000-4002f000 rw-p 00001000 03:05 147482 /lib/libdl-2.3.3.so
4002f000-40171000 r-xp 00000000 03:05 147511 /lib/tls/libc-2.3.3.so
40171000-40174000 rw-p 00142000 03:05 147511 /lib/tls/libc-2.3.3.so
40174000-40177000 rw-p 00000000 00:00 0
40177000-40178000 r–p 00000000 03:05 184085 /usr/share/locale/en_US/LC_NUMERIC
40178000-401a4000 r–p 00000000 03:05 183688 /usr/share/locale/ISO-8859-1/LC_CTYPE
401a4000-401a5000 r-xp 00000000 03:05 180462 /usr/lib/gconv/ISO8859-1.so
401a5000-401a6000 rw-p 00001000 03:05 180462 /usr/lib/gconv/ISO8859-1.so
401b3000-401bd000 r-xp 00000000 03:05 147492 /lib/libnss_files-2.3.3.so
401bd000-401be000 rw-p 00009000 03:05 147492 /lib/libnss_files-2.3.3.so
bfffa000-c0000000 rwxp ffffb000 00:00 0
ffffe000-fffff000 —p 00000000 00:00 0