translated

This commit is contained in:
strugglingyouth 2015-06-13 18:16:20 -07:00
parent 14d06fe758
commit e48fb59106

View File

@ -6,17 +6,11 @@ Linux提供了一个方法来刷新或清除RAM缓存。
每一个Linux系统有三个选项来清除缓存而不中断任何进程或服务。
1仅清除缓存页
<pre class="prettyprint linenums"><code>
sync; echo 1 &gt; /proc/sys/vm/drop_caches
</code></pre>
2清除目录项和inodes
<pre class="prettyprint linenums"><code>
sync; echo 2 &gt; /proc/sys/vm/drop_caches
</code></pre>
3清除,缓存页目录项和inodes
<pre class="prettyprint linenums"><code>
sync; echo 3 &gt; /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 &gt; /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 &gt; /proc/sys/vm/drop_caches"
如何清除Linux的交换空间
如果你想清除掉的空间,你可以运行下面的命令:
<pre class="prettyprint linenums"><code>
# swapoff -a &amp;&amp; swapon -a
</code></pre>
此外了解有关风险后您可能会将上面的命令添加到cron中。
现在我们将上面两种命令结合成一个命令写出正确的脚本来同时清除RAM缓存和交换空间。
<pre class="prettyprint linenums"><code>
# echo 3 &gt; /proc/sys/vm/drop_caches &amp;&amp; swapoff -a &amp;&amp; swapon -a &amp;&amp; printf '\n%s\n' 'Ram-cache and Swap Cleared'
</code></pre>
<pre class="prettyprint linenums"><code>
su -c 'echo 3 &gt;/proc/sys/vm/drop_caches' &amp;&amp; swapoff -a &amp;&amp; swapon -a &amp;&amp; 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 &gt;/proc/sys/vm/drop_caches' &amp;&amp; swapoff -a &amp;&amp; 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>
&nbsp;
&nbsp;