linux中使用free可以查看系统内存使用状态, 默认单位为k blue_stone@blueice:~$ freetotal used free shared buffers cachedMem: 2075320 1879172 196148 0 533484 952588-/+ buffers/cache: 393100 1682220Swap: 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/meminfoMemTotal: 2075320 kBMemFree: 25016 kBBuffers: 763564 kBCached: 879860 kBSwapCached: 0 kBActive: 1056968 kBInactive: 843136 kBHighTotal: 917440 kBHighFree: 13892 kBLowTotal: 1157880 kBLowFree: 11124 kBSwapTotal: 2008084 kBSwapFree: 2008084 kBDirty: 4708 kBWriteback: 0 kBMapped: 707152 kBSlab: 102728 kBCommitLimit: 3045744 kBCommitted_AS: 691808 kBPageTables: 2936 kBVmallocTotal: 114680 kBVmallocUsed: 14800 kBVmallocChunk: 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:31Memory: 2073292k/2097088k available (1493k kernel code, 21648k reserved, 546k data, 196k init, 917440k highmem)Freeing initrd memory: 1079k freedMEM window: f8000000-f80fffffhighmem bounce pool size: 64 pagesFreeing unused kernel memory: 196k freedehci_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/statusName: bashState: S (sleeping)SleepAVG: 98%Tgid: 5346Pid: 5346PPid: 5343TracerPid: 0Uid: 1000 1000 1000 1000Gid: 104 104 104 104FDSize: 256Groups: 6 24 29 44 104 113 1000 1001VmPeak: 6528 kBVmSize: 6528 kBVmLck: 0 kBVmHWM: 1976 kBVmrss: 1976 kBVmData: 752 kBVmStk: 84 kBVmExe: 644 kBVmLib: 1788 kBVmPTE: 16 kBThreads: 1SigQ: 0/4294967295SigPnd: 0000000000000000ShdPnd: 0000000000000000SigBlk: 0000000000000000SigIgn: 0000000000384004SigCgt: 000000004b813efbCapInh: 0000000000000000CapPrm: 0000000000000000CapEff: 0000000000000000 VmSize即为该进程内存总大小, 和top输出中的VIRT一致。 |