free
参考文章:https://www.cnblogs.com/ultranms/p/9254160.html

total
内存总量,等同于 /proc/meminfo 中的 MemTotal 和 SwapTotal

used
已使用的内存。计算公式为:used = total - free - buffers - cache
free
未被使用的物理内存(等同于 /proc/meminfo 中的 MemFree 和 SwapFree)
shared
通常是临时文件系统使用的内存(等同于 /proc/meminfo 中的 Shmem;自内核 2.6.32 版本可用,不可用则显示为 0)
buffer/cache
查阅 man free 和 /proc 得知:
- Buffers %lu
Relatively temporary storage for raw disk blocks that shouldn't get tremendously large (20MB or so).- Cached %lu
In-memory cache for files read from the disk (the page cache). Doesn't include SwapCached.- SReclaimable %lu (since Linux 2.6.19)
Part of Slab, that might be reclaimed, such as caches.- SUnreclaim %lu (since Linux 2.6.19)
Part of Slab, that cannot be reclaimed on memory pressure.
cache
简单说来:
buffer指代磁盘缓存,cache 指代文件系统的缓存。
- 具体查看buffer/cache的大小
# cat /proc/meminfo ... Buffers: 174448 kB Cached: 1914600 kB ...
available
是从应用程序的角度看到的可用内存数量。
Linux 内核为了提升磁盘操作的性能,会消耗一部分内存去缓存磁盘数据,就是我们介绍的 buffer 和 cache。所以对于内核来说,buffer 和 cache 都属于已经被使用的内存。
当应用程序需要内存时,如果没有足够的 free 内存可以用,内核就会从 buffer 和 cache 中回收内存来满足应用程序的请求。
swap
swap space 是磁盘上的一块区域,具体的实现可以是 swap 分区也可以是 swap 文件。
当系统物理内存吃紧时,Linux 会将内存中不常访问的数据保存到 swap 上,这样系统就有更多的物理内存为各个进程服务,而当系统需要访问 swap 上存储的内容时,再将 swap 上的数据加载到内存中,这就是常说的换出和换入。交换空间可以在一定程度上缓解内存不足的情况。
但是它需要读写磁盘数据,所以性能不是很高。
- 禁止swap
现在机器的内存已经足够大,没有必要使用swap,所以,为了性能,一般会禁止掉swap。
让free持续运行一段时间
free -s 3 # 每隔3s执行一次、
vmstat
vmstat w 1

- swpd:使用的虚拟内存数量
- si:一部分内存从swap分区(磁盘)被移动到了内存
- so:一部分内存被移动到了swap分区(磁盘)
- bi:从块设备收到的快数量(磁盘->内存)
- bo:发送到块设备的块数量(内存->磁盘)
利用 vmstat 探究
- 清理文件页,目录项,Inodes等各种缓存
echo 3 > /proc/sys/vm/drop_caches
获取进程内存使用排名
top
- M 按照内存资源使用排序
ps 命令配合sort排序
首先利用ps aux

再利用
sort -k 6
排序。
Out of memory
用dmesg -Tx|grep "Out of memory"
查看是否有进程内存溢出
网友评论