translated

This commit is contained in:
strugglingyouth 2015-06-13 08:42:10 -07:00
parent dc5ff01228
commit 14d06fe758
2 changed files with 88 additions and 100 deletions

View File

@ -1,100 +0,0 @@
translation by strugglingyouth
How to Clear RAM Memory Cache, Buffer and Swap Space on Linux
================================================================================
Like any other operating system, GNU/Linux has implemented a memory management efficiently and even more than that. But if any process is eating away your memory and you want to clear it, Linux provides a way to flush or clear ram cache.
![Clear RAM Cache and Swap in Linux](http://www.tecmint.com/wp-content/uploads/2015/05/Clear-RAM-Cache-in-Linux.jpg)
### How to Clear Cache in Linux? ###
Every Linux System has three options to clear cache without interrupting any processes or services.
1. Clear PageCache only.
# sync; echo 1 > /proc/sys/vm/drop_caches
2. Clear dentries and inodes.
# sync; echo 2 > /proc/sys/vm/drop_caches
3. Clear PageCache, dentries and inodes.
# sync; echo 3 > /proc/sys/vm/drop_caches
Explanation of above command.
sync will flush the file system buffer. Command Separated by `“;”` run sequentially. The shell wait for each command to terminate before executing the next command in the sequence. As mentioned in kernel documentation, writing to drop_cache will clean cache without killing any application/service, [command echo][1] is doing the job of writing to file.
If you have to clear the disk cache, the first command is safest in enterprise and production as `“...echo 1 > ….”` will clear the PageCache only. It is not recommended to use third option above `“...echo 3 >”` in production until you know what you are doing, as it will clear PageCache, dentries and inodes.
**Is it a good idea to free Buffer and Cache in Linux that might be used by Linux Kernel?**
When you are applying various settings and want to check, if it is actually implemented specially on I/O-extensive benchmark, then you may need to clear buffer cache. You can drop cache as explained above without rebooting the System i.e., no downtime required.
Linux is designed in such a way that it looks into disk cache before looking onto the disk. If it finds the resource in the cache, then the request doesnt reach the disk. If we clean the cache, the disk cache will be less useful as the OS will look for the resource on the disk.
Moreover it will also slow the system for a few seconds while the cache is cleaned and every resource required by OS is loaded again in the disk-cache.
Now we will be creating a shell script to auto clear RAM cache daily at 2PM via a cron scheduler task. Create a shell script clearcache.sh and add the following lines.
#!/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"
Set execute permission on the clearcache.sh file.
# chmod 755 clearcache.sh
Now you may call the script whenever you required to clear ram cache.
Now set a cron to clear RAM cache everyday at 2PM. Open crontab for editing.
# crontab -e
Append the below line, save and exit to run it at 2PM daily.
0 3 * * * /path/to/clearcache.sh
For more details on how to cron a job you may like to check our article on [11 Cron Scheduling Jobs][2].
**Is it good idea to auto clear RAM cache on production server?**
No! it is not. Think of a situation when you have scheduled the script to clear ram cache everyday at 2PM. Everyday at 2PM the script is executed and it flushes your RAM cache. One day for whatsoever reason, may be more than expected users are online on your website and seeking resource from your server.
At the same time scheduled script run and clears everything in cache. Now all the user are fetching data from disk. It will result in server crash and corrupt the database. So clear ram-cache only when required,and known your foot steps, else you are a Cargo Cult System Administrator.
#### How to Clear Swap Space in Linux? ####
If you want to clear Swap space, you may like to run the below command.
# swapoff -a && swapon -a
Also you may add above command to a cron script above, after understanding all the associated risk.
Now we will be combining both above commands into one single command to make a proper script to clear RAM Cache and Swap Space.
# echo 3 > /proc/sys/vm/drop_caches && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'
OR
su -c 'echo 3 >/proc/sys/vm/drop_caches' && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'
After testing both above command, we will run command “free -h” before and after running the script and will check cache.
![Clear RAM Cache and Swap Space](http://www.tecmint.com/wp-content/uploads/2015/05/Clear-RAM-Cache.gif)
Thats all for now, if you liked the article, dont forget to provide us with your valuable feedback in the comments to let us know, what you think is it a good idea to clear ram cache and buffer in production and Enterprise?
--------------------------------------------------------------------------------
via: http://www.tecmint.com/clear-ram-memory-cache-buffer-and-swap-space-on-linux/
作者:[Avishek Kumar][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.tecmint.com/author/avishek/
[1]:http://www.tecmint.com/echo-command-in-linux/
[2]:http://www.tecmint.com/11-cron-scheduling-task-examples-in-linux/

View File

@ -0,0 +1,88 @@
像任何其他的操作系统一样GNU / Linux已经有效地实施了内存管理甚至更多。但是如果有任何进程正在蚕食你的内存你要清除它
Linux提供了一个方法来刷新或清除RAM缓存。
在Linux中如何清除缓存
每一个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命令做写入文件的工作。
如果你必须清除磁盘高速缓存,第一个命令是最安全在企业和生产环境中,“...echo 1&gt; ...”只会清除页缓存。
不建议使用上面第三个选项在生产环境中“...echo 3 &gt;除非你明确自己在做什么因为它会清除缓存页目录项和inodes。
在Linux上释放Buffer和Cache要用到内核是否是个好主意
当你请求许多设定想要检查时如果它实际上是专门实施对I/O 广泛的基准测试,那么你可能需要清除缓存。你可以如上所示删除缓存,无需重新启动系统即无需停机。
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是否是一个好主意
它不是。想想一个情况当你已经预定脚本来清除RAM缓存每天在下午2点。每天下午2点该脚本会执行并刷新你的RAM缓存。在一天中的任何时候您网站用户的在线量可能会超过预期的并从你的服务器请求资源。同时调度器运行着脚本并在高速缓存中清除一切。当所有的用户都从磁盘读取数据时这将导致服务器崩溃并损坏数据库。
因此清除缓存仅在必要时并且在你的预料之中否则你就是个Cargo Cult System Administrator。
如何清除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>
就是现在,如果你喜欢这篇文章,不要忘记向我们提供您宝贵的意见,
让我们知道您认为在企业和生产环境中清除RAM缓存和缓冲区是否是一个好主意
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;