diff --git a/translated/tech/20150610_How_to_Clear_RAM_Memory_Cache_Buffer_and_Swap_Space_on_Linux.md b/translated/tech/20150610_How_to_Clear_RAM_Memory_Cache_Buffer_and_Swap_Space_on_Linux.md index 932bc11cd2..bfe689602d 100644 --- a/translated/tech/20150610_How_to_Clear_RAM_Memory_Cache_Buffer_and_Swap_Space_on_Linux.md +++ b/translated/tech/20150610_How_to_Clear_RAM_Memory_Cache_Buffer_and_Swap_Space_on_Linux.md @@ -6,17 +6,11 @@ Linux提供了一个方法来刷新或清除RAM缓存。 每一个Linux系统有三个选项来清除缓存而不中断任何进程或服务。 1,仅清除缓存页 -
sync; echo 1 > /proc/sys/vm/drop_caches
-
2,清除目录项和inodes
-
sync; echo 2 > /proc/sys/vm/drop_caches
-
3,清除,缓存页,目录项和inodes
-
sync; echo 3 > /proc/sys/vm/drop_caches
-
上述命令的说明:
sync将刷新文件系统缓存,命令通过“;”分隔,顺序执行,shell等待终止在序列中的每一个命令执行之前。正如内核文档中提到的,写到drop_cache将清空缓存而不杀死任何应用程序/服务,echo命令做写入文件的工作。
@@ -34,25 +28,17 @@ Linux被设计成它在寻找磁盘之前到磁盘缓存寻找的方式。如果
现在,我们将通过一个cron任务调度器创建一个shell脚本在每天下午2点自动清除RAM缓存。
创建一个shell脚本clearcache.sh并在其中添加以下行:
-
#!/bin/bash
# Note, we are using "echo 3", but it is not recommended in production instead use "echo 1"
echo "echo 3 > /proc/sys/vm/drop_caches"
-
给clearcache.sh文件设置执行权限
-
# chmod 755 clearcache.sh
-
现在,当你需要清除RAM缓存时只需要调用脚本。
现在设置一个定时任务来清除RAM缓存每天在下午2点,打开crontab进行编辑。
-
# crontab -e
-
添加以下行,保存并退出。
-
0 3 * * * /path/to/clearcache.sh
-
有关如何创建一个定时任务,更多细节你可以查看我们的文章11 Cron Scheduling Jobs。
在生产环境的服务器上自动清除RAM是否是一个好主意?
@@ -63,19 +49,13 @@ echo "echo 3 > /proc/sys/vm/drop_caches"
如何清除Linux的交换空间?
如果你想清除掉的空间,你可以运行下面的命令:
-
# swapoff -a && swapon -a
-
此外,了解有关风险后,您可能会将上面的命令添加到cron中。
现在,我们将上面两种命令结合成一个命令写出正确的脚本来同时清除RAM缓存和交换空间。
-
# echo 3 > /proc/sys/vm/drop_caches && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'
-
或
-
su -c 'echo 3 >/proc/sys/vm/drop_caches' && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'
-
在测试上面的命令之前,我们先运行“free -m” 然后执行脚本检查缓存。