gdb 2008-03-07
Linux中使用free可以查看系统内存使用状态, 默认单位为k
blue_stone@blueice:~$ free
total used free shared buffers cached
Mem: 2075320 1879172 196148 0 533484 952588
-/+ buffers/cache: 393100 1682220
Swap: 2008084 0 2008084
Mem 行显示了从系统角度看来内存使用的情况, total是系统可用的内存大小, 数量上等于系统物理内存减去内核保留的内存. buffers和cached是系统用做缓冲的内存. buffers与某个块设备关联, 包含了文件系统元数据, 并且跟踪了块的变化. cache只包含了文件本身.
-/+ buffers/cache行则从用户角度显示内存信息, 可用内存从数量上等于mem行used列值减去buffers和cached内存的大小. 因为buffers和cached是操作系统为加快系统运行而设置的, 当用户需要时, 可以只接为用户使用.
top和vmstat也显示了系统内存的信息, 和free的显示结果类似.
这些命令都是从/proc/meminfo中读取内存信息.
blue_stone@blueice:~$ cat /proc/meminfo
MemTotal: 2075320 kB
MemFree: 25016 kB
Buffers: 763564 kB
Cached: 879860 kB
SwapCached: 0 kB
Active: 1056968 kB
Inactive: 843136 kB
HighTotal: 917440 kB
HighFree: 13892 kB
LowTotal: 1157880 kB
LowFree: 11124 kB
SwapTotal: 2008084 kB
SwapFree: 2008084 kB
Dirty: 4708 kB
Writeback: 0 kB
Mapped: 707152 kB
Slab: 102728 kB
CommitLimit: 3045744 kB
Committed_AS: 691808 kB
PageTables: 2936 kB
VmallocTotal: 114680 kB
VmallocUsed: 14800 kB
VmallocChunk: 98800 kB
关于/proc/meminfo中各行的含意, 在内核源代码的Documentation/filesystems/proc.txt文件中有叙述
系统的真实内存大小可以用dmesg |grep [mM][eE][mM]看到
blue_stone@blueice:~$ dmesg |grep [mM][eE][mM]
895MB HIGHMEM available.
1152MB LOWMEM available.
HighMem zone: 229360 pages, LIFO batch:31
Memory: 2073292k/2097088k available (1493k kernel code, 21648k reserved, 546k data, 196k init, 917440k highmem)
Freeing initrd memory: 1079k freed
MEM window: f8000000-f80fffff
highmem bounce pool size: 64 pages
Freeing unused kernel memory: 196k freed
ehci_hcd 0000:00:10.4: irq 177, io mem 0xf8121000
/dev/vmnet: hub 0 does not exist, allocating memory.
[fglrx] Maximum main memory to use for locked dma buffers: 1898 MBytes.
/dev/vmnet: hub 3 does not exist, allocating memory.
/dev/vmnet: hub 1 does not exist, allocating memory.
/dev/vmnet: hub 2 does not exist, allocating memory.
进程使用的内存可以用top, ps来查看top中有3个列 VIRT RES SHR, 标示了进程使用的内存情况, VIRT标识这个进程可以使用的内存总大小, 包括这个进程真实使用的内存, 映射过的文件, 和别的进程共享的内存等. RES标识这个这个进程真实占用内存的大小. SHR标识可以和别的进程共享的内存和库大小.
/proc/pid/status显示一个进程的详细状态, 下面是一个示例:
blue_stone@blueice:~$ cat /proc/5346/status
Name: bash
State: S (sleeping)
SleepAVG: 98%
Tgid: 5346
Pid: 5346
PPid: 5343
TracerPid: 0
Uid: 1000 1000 1000 1000
Gid: 104 104 104 104
FDSize: 256
Groups: 6 24 29 44 104 113 1000 1001
VmPeak: 6528 kB
VmSize: 6528 kB
VmLck: 0 kB
VmHWM: 1976 kB
VmRSS: 1976 kB
VmData: 752 kB
VmStk: 84 kB
VmExe: 644 kB
VmLib: 1788 kB
VmPTE: 16 kB
Threads: 1
SigQ: 0/4294967295
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000384004
SigCgt: 000000004b813efb
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
VmSize即为该进程内存总大小, 和top输出中的VIRT一致.