linux手动释放资源

查看内存占用情况:free

free
              total        used        free      shared  buff/cache   available
Mem:        1863252      775764      249672       14140      837816      863760
Swap:       2097148           0     2097148

默认kb为单位,使用-m,-g参数可以改为mb,gb参数

free -m
              total        used        free      shared  buff/cache   available
Mem:           1819         758         242          13         818         842
Swap:          2047           0        2047

占用内存的一般是文件系统的缓存,这些缓存刷入磁盘不会影响读写数据。所以可以手动释放

sync 命令将所有未写的系统缓冲区写到磁盘中,具体可以使用sync --help或者man sync查看说明

释放内存

echo 3 > /proc/sys/vm/drop_caches

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

# echo 1 > /proc/sys/vm/drop_caches; free pagecache, use
# echo 2 > /proc/sys/vm/drop_caches; free dentries and inodes
# echo 3 > /proc/sys/vm/drop_caches; free pagecache, dentries and inodes

参考proc目录下文件说明

http://man7.org/linux/man-pages/man5/proc.5.html

总体来说,手动释放内存占用需要同时执行如下两个命令:

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

执行完之后可以再次free -m查看内存占用情况

sync 将未写的缓存刷入磁盘

扫描二维码关注公众号,回复: 9186324 查看本文章

echo N > /proc/sys/vm/drop_caches释放缓存

Writing to this file causes the kernel to drop clean caches, dentries, and inodes from memory, causing that memory to become free

查看文件句柄占用数量

lsof -c php

查看php占用的文件句柄信息,包括php、php-fpm,-c参数是获取所有php开头的进程占用文件信息

lsof -c php | wc可以统计行数,也就是占用的文件的个数,

如果文件占用数量太大,也没有更好的办法,只能重启php-fpm,找到文件占用过高的问题出处,有open的地方一定要close,

查看其他资源占用情况,

top

按q退出,top动态显示当前进程占用的资源列表,网络、内存、cpu占用情况,

发布了275 篇原创文章 · 获赞 46 · 访问量 28万+

猜你喜欢

转载自blog.csdn.net/youyudexiaowangzi/article/details/96307119
今日推荐