mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-25 00:50:15 +08:00
translated
This commit is contained in:
parent
14d06fe758
commit
e48fb59106
@ -6,17 +6,11 @@ Linux提供了一个方法来刷新或清除RAM缓存。
|
||||
每一个Linux系统有三个选项来清除缓存而不中断任何进程或服务。
|
||||
|
||||
1,仅清除缓存页
|
||||
<pre class="prettyprint linenums"><code>
|
||||
sync; echo 1 > /proc/sys/vm/drop_caches
|
||||
</code></pre>
|
||||
2,清除目录项和inodes
|
||||
<pre class="prettyprint linenums"><code>
|
||||
sync; echo 2 > /proc/sys/vm/drop_caches
|
||||
</code></pre>
|
||||
3,清除,缓存页,目录项和inodes
|
||||
<pre class="prettyprint linenums"><code>
|
||||
sync; echo 3 > /proc/sys/vm/drop_caches
|
||||
</code></pre>
|
||||
上述命令的说明:
|
||||
|
||||
sync将刷新文件系统缓存,命令通过“;”分隔,顺序执行,shell等待终止在序列中的每一个命令执行之前。正如内核文档中提到的,写到drop_cache将清空缓存而不杀死任何应用程序/服务,echo命令做写入文件的工作。
|
||||
@ -34,25 +28,17 @@ Linux被设计成它在寻找磁盘之前到磁盘缓存寻找的方式。如果
|
||||
|
||||
现在,我们将通过一个cron任务调度器创建一个shell脚本在每天下午2点自动清除RAM缓存。
|
||||
创建一个shell脚本clearcache.sh并在其中添加以下行:
|
||||
<pre class="prettyprint linenums"><code>
|
||||
#!/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"
|
||||
</code></pre>
|
||||
给clearcache.sh文件设置执行权限
|
||||
<pre class="prettyprint linenums"><code>
|
||||
# chmod 755 clearcache.sh
|
||||
</code></pre>
|
||||
现在,当你需要清除RAM缓存时只需要调用脚本。
|
||||
|
||||
现在设置一个定时任务来清除RAM缓存每天在下午2点,打开crontab进行编辑。
|
||||
<pre class="prettyprint linenums"><code>
|
||||
# crontab -e
|
||||
</code></pre>
|
||||
添加以下行,保存并退出。
|
||||
<pre class="prettyprint linenums"><code>
|
||||
0 3 * * * /path/to/clearcache.sh
|
||||
</code></pre>
|
||||
有关如何创建一个定时任务,更多细节你可以查看我们的文章<a href="http://www.tecmint.com/11-cron-scheduling-task-examples-in-linux/">11 Cron Scheduling Jobs</a>。
|
||||
|
||||
在生产环境的服务器上自动清除RAM是否是一个好主意?
|
||||
@ -63,19 +49,13 @@ echo "echo 3 > /proc/sys/vm/drop_caches"
|
||||
如何清除Linux的交换空间?
|
||||
|
||||
如果你想清除掉的空间,你可以运行下面的命令:
|
||||
<pre class="prettyprint linenums"><code>
|
||||
# swapoff -a && swapon -a
|
||||
</code></pre>
|
||||
此外,了解有关风险后,您可能会将上面的命令添加到cron中。
|
||||
|
||||
现在,我们将上面两种命令结合成一个命令写出正确的脚本来同时清除RAM缓存和交换空间。
|
||||
<pre class="prettyprint linenums"><code>
|
||||
# echo 3 > /proc/sys/vm/drop_caches && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'
|
||||
</code></pre>
|
||||
或
|
||||
<pre class="prettyprint linenums"><code>
|
||||
su -c 'echo 3 >/proc/sys/vm/drop_caches' && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'
|
||||
</code></pre>
|
||||
在测试上面的命令之前,我们先运行“free -m” 然后执行脚本检查缓存。
|
||||
|
||||
<a href="https://camo.githubusercontent.com/659439c8dbef449fa82ba64ff5a02dc0e9324017/687474703a2f2f7777772e7465636d696e742e636f6d2f77702d636f6e74656e742f75706c6f6164732f323031352f30352f436c6561722d52414d2d43616368652e676966" target="_blank"><img src="https://camo.githubusercontent.com/659439c8dbef449fa82ba64ff5a02dc0e9324017/687474703a2f2f7777772e7465636d696e742e636f6d2f77702d636f6e74656e742f75706c6f6164732f323031352f30352f436c6561722d52414d2d43616368652e676966" alt="Clear RAM Cache and Swap Space" data-canonical-src="http://www.tecmint.com/wp-content/uploads/2015/05/Clear-RAM-Cache.gif" /></a>
|
||||
@ -85,4 +65,4 @@ su -c 'echo 3 >/proc/sys/vm/drop_caches' && swapoff -a && swa
|
||||
|
||||
via:<a href="http://www.tecmint.com/clear-ram-memory-cache-buffer-and-swap-space-on-linux/">http://www.tecmint.com/clear-ram-memory-cache-buffer-and-swap-space-on-linux/</a>
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user