linux 手动释放内存
以下是摘自宝塔释放内存的脚本:
if [ -f "/etc/init.d/php-fpm-74" ];then
/etc/init.d/php-fpm-74 reload
fi
if [ -f "/etc/init.d/mysqld" ];then
/etc/init.d/mysqld reload
fi
if [ -f "/etc/init.d/nginx" ];then
/etc/init.d/nginx reload
fi
if [ -f "/etc/init.d/httpd" ];then
/etc/init.d/httpd graceful
fi
if [ -f "/etc/init.d/pure-ftpd" ];then
pkill -9 pure-ftpd
sleep 0.3
/etc/init.d/pure-ftpd start 2>/dev/null
fi
sync
sleep 2
sync
echo 3 > /proc/sys/vm/drop_cachessync 命令:sync 指令会将存于 buffer 中的资料强制写入硬盘中
Linux 系统中欲写入硬盘的资料有的时候为了效率起见,会写到 filesystem buffer 中,这个 buffer 是一块记忆体空间,如果欲写入硬盘的资料存于此 buffer 中,而系统又突然断电的话,那么资料就会流失了。
/proc/sys/vm/drop_caches的值,默认为0
来自官方的解释:
/proc/sys/vm/drop_caches (since Linux 2.6.16)
Writing to this file causes the kernel to drop clean caches,
dentries and inodes from memory, causing that memory to become
free.
To free pagecache, use echo 1 > /proc/sys/vm/drop_caches; to
free dentries and inodes, use echo 2 > /proc/sys/vm/drop_caches;
to free pagecache, dentries and inodes, use echo 3 >
/proc/sys/vm/drop_caches.
Because this is a non-destructive operation and dirty objects
are not freeable, the user should run sync first.写入此文件会导致内核丢弃内存中的 dentry 和 inode缓存的数据,导致内存被释放。
释放页面缓存,请使用
echo 1 > /proc/sys/vm/drop_caches释放 dentries 和 inode,使用
echo 2 > /proc/sys/vm/drop_caches释放 pagecache、dentries 和 inode,请使用
echo 3 > /proc/sys/vm/drop_caches使用前,请执行sync命令,将 buffer 中的数据写入磁盘