free 查看内存容量

  1. free -h
  2. # Mem的阈值是80%,Swap的阈值是5%,只有当物理内存不够用的时候,才会使用Swap,
  3. # 所以当Swap被使用了,就说明物理内存已经不够了
  4. # 使用top即可定位是哪个进程占用的

vmstat 内存监控,监控的整个服务器的内存变化

用法

  1. ~ vmstat -h
  2. Usage:
  3. vmstat [options] [delay [count]]
  4. Options:
  5. -a, --active active/inactive memory
  6. -f, --forks number of forks since boot
  7. -m, --slabs slabinfo
  8. -n, --one-header do not redisplay header
  9. -s, --stats event counter statistics
  10. -d, --disk disk statistics
  11. -D, --disk-sum summarize disk statistics
  12. -p, --partition <dev> partition specific statistics
  13. -S, --unit <char> define display unit
  14. -w, --wide wide output
  15. -t, --timestamp show timestamp
  16. -h, --help display this help and exit
  17. -V, --version output version information and exit

每个1秒刷新一次

  1. tiffin@iZ2ze1l5ld7n18s5p8meceZ: vmstat -S m
  2. procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
  3. r b swpd free buff cache si so bi bo in cs us sy id wa st
  4. 0 0 0 4577 161 9496 0 0 292 184 1074 731 2 0 98 0 0
  5. # buff: 读
  6. # cache: 写

查看进程的内存

用法

  1. ~ smem -h
  2. Usage: smem [options]
  3. Options:
  4. -h, --help show this help message and exit
  5. -H, --no-header disable header line
  6. -c COLUMNS, --columns=COLUMNS
  7. columns to show
  8. -t, --totals show totals
  9. -a, --autosize size columns to fit terminal size
  10. -R REALMEM, --realmem=REALMEM
  11. amount of physical RAM
  12. -K KERNEL, --kernel=KERNEL
  13. path to kernel image
  14. -m, --mappings show mappings
  15. -u, --users show users
  16. -w, --system show whole system
  17. -P PROCESSFILTER, --processfilter=PROCESSFILTER
  18. process filter regex
  19. -M MAPFILTER, --mapfilter=MAPFILTER
  20. map filter regex
  21. -U USERFILTER, --userfilter=USERFILTER
  22. user filter regex
  23. -n, --numeric numeric output
  24. -s SORT, --sort=SORT field to sort on
  25. -r, --reverse reverse sort
  26. -p, --percent show percentage
  27. -k, --abbreviate show unit suffixes
  28. --pie=PIE show pie graph
  29. --bar=BAR show bar graph
  30. -S SOURCE, --source=SOURCE
  31. /proc data source

查看某个进程的内存使用情况

  1. # 按照uss排序
  2. ~ smem -p -s uss
  3. # PSS: 把一个共享库占用的内存分摊到使用了这个共享库的各个进程上
  4. # RSS:把一个共享库占用的内存直接加到使用了这个共享库的进程上
  5. # USS:进程独自占用的物理内存

内存手动回收

/proc/sys/vm/drop_caches默认值是0

Clear PageCache only

# sync; echo 1 > /proc/sys/vm/drop_caches

Clear dentries and inodes

# sync; echo 2 > /proc/sys/vm/drop_caches

Clear pagecache, dentries, and inodes

# sync; echo 3 > /proc/sys/vm/drop_caches

Note:

  • sync will flush the file system buffer. (将所有未写的系统缓冲区写到磁盘中,包含已修改的 i-node、已延迟的块 I/O 和读写映射文件)
  • 写入后只会执行一次,后续不用再去管它,不需要写入0去清除什么的(其实你写入0也是不会成功的),如果非强迫症犯了想把它清掉,那就写入4好了,4是”100”,末尾两位是0就行。
  • 参考