Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1389290
  • 博文数量: 120
  • 博客积分: 182
  • 博客等级: 入伍新兵
  • 技术积分: 2278
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-19 16:31
文章分类

全部博文(120)

文章存档

2015年(12)

2014年(13)

2013年(40)

2012年(55)

分类: LINUX

2014-04-04 21:02:50

平时不是很经常使用这个命令,但是最近机子反应真的很慢,不知道怎么回事,所以先对系统内存查看了下,用了free命令,记录下来吧。也好记得深刻些~
  1. free - Display amount of free and used memory in the system
free命令用来显示linux系统中总的物理内存,已用的物理内存,空闲的物理内存,swap内存,被kernel使用的内存buffer。此刻,我的机子中物理内存的使用情况:
  1. root@Lenovo:/home/helianthus# free
  2.                 total         used        free    shared     buffers   cached
  3. Mem: 1987540  1837588  149952       0          38528    317616
  4. -/+ buffers/cache: 1481444 506096
  5. Swap: 3998716 42136 3956580
单位都是kb。
total:总的物理内存大小
used:已使用量
free:空闲内存
shared:多少个进程共享的内存总量
buffers/cached:磁盘缓存的大小
第二行(mem)的used/free和第三行(-/+ buffers/cache)的used/free的区别,这两行的区别在于看的角度不同哦:
第二行是从OS的角度来看,因为对于OS,buffers/cached 都是属于被使用,所以其可用内存是149952KB,已用内存是1837588KB,其中包括,内核(OS)使用+Application(X, web,Qt,etc)使用的+buffers+cached.
第三行所指的是从应用程序角度来看,对于应用程序来说,buffers/cached 相当于是可用的,因为buffers/cached是为了提高文件读取的性能,当应用程序需在用到内存的时候,buffers/cached会很快地被回收。所以从应用程序的角度来说,可用内存=系统free memory+buffers+cached。比如,上面的506096=149952 +38528 + 317616

什么时候进行memory swap,怎么swap?
当可用内存小于某个阈值时就会触发swap.而这些所谓的阈值用户可以通过访问/proc/meminfo了解:
点击(此处)折叠或打开
  1. root@Lenovo:/home/helianthus# cat /proc/meminfo
  2. MemTotal: 1987540 kB
  3. MemFree: 160320 kB
  4. Buffers: 49696 kB
  5. Cached: 332844 kB
  6. SwapCached: 19556 kB
  7. Active: 1059080 kB
  8. Inactive: 639356 kB
  9. Active(anon): 872172 kB
  10. Inactive(anon): 452880 kB
  11. Active(file): 186908 kB
  12. Inactive(file): 186476 kB
  13. Unevictable: 16 kB
  14. Mlocked: 16 kB
  15. HighTotal: 1109248 kB
  16. HighFree: 16624 kB
  17. LowTotal: 878292 kB
  18. LowFree: 143696 kB
  19. SwapTotal: 3998716 kB
  20. SwapFree: 3946196 kB
  21. Dirty: 52 kB
  22. Writeback: 0 kB
  23. AnonPages: 1299264 kB
  24. Mapped: 143344 kB
  25. Shmem: 9156 kB
  26. Slab: 58856 kB
  27. SReclaimable: 36612 kB
  28. SUnreclaim: 22244 kB
  29. KernelStack: 4568 kB
  30. PageTables: 20036 kB
  31. NFS_Unstable: 0 kB
  32. Bounce: 0 kB
  33. WritebackTmp: 0 kB
  34. CommitLimit: 4992484 kB
  35. Committed_AS: 5098636 kB
  36. VmallocTotal: 122880 kB
  37. VmallocUsed: 53968 kB
  38. VmallocChunk: 65900 kB
  39. HardwareCorrupted: 0 kB
  40. AnonHugePages: 0 kB
  41. HugePages_Total: 0
  42. HugePages_Free: 0
  43. HugePages_Rsvd: 0
  44. HugePages_Surp: 0
  45. Hugepagesize: 2048 kB
  46. DirectMap4k: 8184 kB
  47. DirectMap2M: 905216 kB
free参数:

点击(此处)折叠或打开

  1. -b Display the amount of memory in bytes.
  2.  -c count 
  3.      Display the result count times. Requires the -s option.
  4.  -g Display the amount of memory in gigabytes.
  5.   -k Display the amount of memory in kilobytes. This is the default.
  6.   -l Show detailed low and high memory statistics.
  7.  -m Display the amount of memory in megabytes.
  8. -o Display the output in old format, the only difference being this option will disable the display of the "buffer adjusted" line.
  9.  -s Continuously display the result delay seconds apart. You may actually specify any floating point number for delay, usleep(3) is used for microsecond resolution delay times.
  10.  -t Display a line showing the column totals.
  11.    -V Display version information.
如果觉得看内存使用情况时,用kb太麻烦,可以使用free -m以百万字节(megabyte的复数)为单位显示。
如果想看看内核镜像大小:

点击(此处)折叠或打开

  1. root@Lenovo:/home/helianthus# ll -h /proc/kcore
  2. -r-------- 1 root root 1020M 4月 4 20:41 /proc/kcore
(注ls -h, --human-readable with -l, print sizes in human readable format (e.g., 1K 234M 2G)

如果想要看看某个进程占用了多大的内存,在/proc/下都可以获得。(top也能获得好多内存使用的相关信息)
  1. /proc/meminfo 机器的内存使用信息
  2. /proc/pid/maps pid为进程号,显示当前进程所占用的虚拟地址。
  3. /proc/pid/statm 进程所占用的内存
  1. root@Lenovo:/proc# cat 1970/statm
  2. 15518 1254 948 16 0 9422 0

每列的数据代表的含义:

  1. proc/[pid]/statm Provides information about memory usage, measured in pages. The
  2.               columns are:

  3.                   size total program size
  4.                              (same as VmSize in /proc/[pid]/status)
  5.                   resident resident set size
  6.                              (same as VmRSS in /proc/[pid]/status)
  7.                   share shared pages (from shared mappings)
  8.                   text text (code)
  9.                   lib library (unused in Linux 2.6)
  10.                   data data + stack
  11.                   dt dirty pages (unused in Linux 2.
还可以通过/proc/pid$$/status文件更详细的查看某个进程的内存使用情况。

我们通过free命令查看机器空闲内存时,会发现free的值很小。这主要是因为,在Linux系统中有这么一种思想,内存不用白不用,因此它尽可能的
cache和buffer一些数据,以方便下次使用。但实际上这些内存也是可以立刻拿来使用的。

所以 空闲内存=free+buffers+cached=total-used

查看一个进程使用的内存,是一个很令人困惑的事情。因为我们写的程序,必然要用到动态链接库,将其加入到自己的地址空间中,但是/proc/pid/statm统计出来的数据,会将这些动态链接库所占用的内存也简单的算进来。这样带来的问题,动态链接库占用的内存有些是其他程序使用时占用的,却算在了你这里。你的程序中包含了子进程,那么有些动态链接库重用的内存会被重复计算。因此要想准确的评估一个程序所占用的内存是十分困难的,通过写一个module的方式,来准确计算某一段虚拟地址所占用的内存,可能对我们有用。


from:http://blog.csdn.net/repeaterbin/article/details/3086365
阅读(2180) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~