free 查看内存容量
free -h# Mem的阈值是80%,Swap的阈值是5%,只有当物理内存不够用的时候,才会使用Swap,# 所以当Swap被使用了,就说明物理内存已经不够了# 使用top即可定位是哪个进程占用的
vmstat 内存监控,监控的整个服务器的内存变化
用法
➜ ~ vmstat -hUsage:vmstat [options] [delay [count]]Options:-a, --active active/inactive memory-f, --forks number of forks since boot-m, --slabs slabinfo-n, --one-header do not redisplay header-s, --stats event counter statistics-d, --disk disk statistics-D, --disk-sum summarize disk statistics-p, --partition <dev> partition specific statistics-S, --unit <char> define display unit-w, --wide wide output-t, --timestamp show timestamp-h, --help display this help and exit-V, --version output version information and exit
每个1秒刷新一次
tiffin@iZ2ze1l5ld7n18s5p8meceZ: vmstat -S mprocs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----r b swpd free buff cache si so bi bo in cs us sy id wa st0 0 0 4577 161 9496 0 0 292 184 1074 731 2 0 98 0 0# buff: 读# cache: 写
查看进程的内存
用法
➜ ~ smem -hUsage: smem [options]Options:-h, --help show this help message and exit-H, --no-header disable header line-c COLUMNS, --columns=COLUMNScolumns to show-t, --totals show totals-a, --autosize size columns to fit terminal size-R REALMEM, --realmem=REALMEMamount of physical RAM-K KERNEL, --kernel=KERNELpath to kernel image-m, --mappings show mappings-u, --users show users-w, --system show whole system-P PROCESSFILTER, --processfilter=PROCESSFILTERprocess filter regex-M MAPFILTER, --mapfilter=MAPFILTERmap filter regex-U USERFILTER, --userfilter=USERFILTERuser filter regex-n, --numeric numeric output-s SORT, --sort=SORT field to sort on-r, --reverse reverse sort-p, --percent show percentage-k, --abbreviate show unit suffixes--pie=PIE show pie graph--bar=BAR show bar graph-S SOURCE, --source=SOURCE/proc data source
查看某个进程的内存使用情况
# 按照uss排序➜ ~ smem -p -s uss# PSS: 把一个共享库占用的内存分摊到使用了这个共享库的各个进程上# RSS:把一个共享库占用的内存直接加到使用了这个共享库的进程上# USS:进程独自占用的物理内存
内存手动回收
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就行。
- 参考
