{"id":12627,"date":"2019-03-28T04:30:28","date_gmt":"2019-03-28T04:30:28","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=12627"},"modified":"2019-03-28T04:30:28","modified_gmt":"2019-03-28T04:30:28","slug":"how-to-clear-ram-memory-cache-buffer-and-swap-space-on-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/28\/how-to-clear-ram-memory-cache-buffer-and-swap-space-on-linux\/","title":{"rendered":"How to Clear RAM Memory Cache, Buffer and Swap Space on Linux"},"content":{"rendered":"<p>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.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/05\/Clear-RAM-Cache-in-Linux.jpg\"><img decoding=\"async\" class=\"size-medium wp-image-13521\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/05\/Clear-RAM-Cache-in-Linux.jpg\" sizes=\"(max-width: 720px) 100vw, 720px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/05\/Clear-RAM-Cache-in-Linux.jpg 720w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/05\/Clear-RAM-Cache-in-Linux-620x297.jpg 620w\" alt=\"Clear RAM Cache and Swap in Linux\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<h4>How to Clear Cache in Linux?<\/h4>\n<p>Every Linux System has three options to clear cache without interrupting any processes or services.<\/p>\n<p><strong>1.<\/strong>\u00a0Clear PageCache only.<\/p>\n<pre># sync; echo 1 &gt; \/proc\/sys\/vm\/drop_caches<\/pre>\n<p><strong>2.<\/strong>\u00a0Clear dentries and inodes.<\/p>\n<pre># sync; echo 2 &gt; \/proc\/sys\/vm\/drop_caches\r\n<\/pre>\n<p><strong>3.<\/strong>\u00a0Clear PageCache, dentries and inodes.<\/p>\n<pre># sync; echo 3 &gt; \/proc\/sys\/vm\/drop_caches \r\n<\/pre>\n<p>Explanation of above command.<\/p>\n<p><strong>sync<\/strong>\u00a0will flush the file system buffer. Command Separated by\u00a0<code>\u201c;\u201d<\/code>\u00a0run sequentially. The shell wait for each command to terminate before executing the next command in the sequence. As mentioned in kernel documentation, writing to\u00a0<strong>drop_cache<\/strong>\u00a0will clean cache without killing any application\/service,\u00a0<a href=\"https:\/\/www.tecmint.com\/echo-command-in-linux\/\" target=\"_blank\" rel=\"noopener\">command echo<\/a>\u00a0is doing the job of writing to file.<\/p>\n<p>If you have to clear the disk cache, the first command is safest in enterprise and production as\u00a0<code>\u201c...echo 1 &gt; \u2026.\u201d<\/code>\u00a0will clear the\u00a0<strong>PageCache<\/strong>\u00a0only. It is not recommended to use third option above\u00a0<code>\u201c...echo 3 &gt;\u201d<\/code>\u00a0in production until you know what you are doing, as it will clear\u00a0<strong>PageCache<\/strong>,\u00a0<strong>dentries<\/strong>\u00a0and\u00a0<strong>inodes<\/strong>.<\/p>\n<h6>Is it a good idea to free Buffer and Cache in Linux that might be used by Linux Kernel?<\/h6>\n<p>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.<\/p>\n<p>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 doesn\u2019t 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.<\/p>\n<p>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.<\/p>\n<p>Now we will be creating a shell script to auto clear RAM cache daily at\u00a0<strong>2am<\/strong>\u00a0via a cron scheduler task. Create a shell script\u00a0<strong>clearcache.sh<\/strong>\u00a0and add the following lines.<\/p>\n<pre>#!\/bin\/bash\r\n# Note, we are using \"echo 3\", but it is not recommended in production instead use \"echo 1\"\r\necho \"echo 3 &gt; \/proc\/sys\/vm\/drop_caches\"\r\n<\/pre>\n<p>Set execute permission on the\u00a0<strong>clearcache.sh<\/strong>\u00a0file.<\/p>\n<pre># chmod 755 clearcache.sh\r\n<\/pre>\n<p>Now you may call the script whenever you required to clear ram cache.<\/p>\n<p>Now set a cron to clear RAM cache everyday at 2am. Open\u00a0<strong>crontab<\/strong>\u00a0for editing.<\/p>\n<pre># crontab -e\r\n<\/pre>\n<p>Append the below line, save and exit to run it at\u00a0<strong>2am<\/strong>\u00a0daily.<\/p>\n<pre>0  2  *  *  *  \/path\/to\/clearcache.sh\r\n<\/pre>\n<p>For more details on how to cron a job you may like to check our article on\u00a0<a href=\"https:\/\/www.tecmint.com\/11-cron-scheduling-task-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">11 Cron Scheduling Jobs<\/a>.<\/p>\n<h6>Is it good idea to auto clear RAM cache on production server?<\/h6>\n<p>No! it is not. Think of a situation when you have scheduled the script to clear ram cache everyday at\u00a0<strong>2am<\/strong>. Everyday at 2am 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.<\/p>\n<p>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.<\/p>\n<h4>How to Clear Swap Space in Linux?<\/h4>\n<p>If you want to clear Swap space, you may like to run the below command.<\/p>\n<pre># swapoff -a &amp;&amp; swapon -a\r\n<\/pre>\n<p>Also you may add above command to a cron script above, after understanding all the associated risk.<\/p>\n<p>Now we will be combining both above commands into one single command to make a proper script to clear RAM Cache and Swap Space.<\/p>\n<pre># 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'\r\n\r\nOR\r\n\r\n$ 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'\" root\r\n<\/pre>\n<p>After testing both above command, we will run command \u201c<strong>free -h<\/strong>\u201d before and after running the script and will check cache.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/05\/Clear-RAM-Cache.gif\"><img decoding=\"async\" class=\"size-medium wp-image-13519\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/05\/Clear-RAM-Cache.gif\" alt=\"Clear RAM Cache and Swap Space\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p>That\u2019s all for now, if you liked the article, don\u2019t 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?<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/clear-ram-memory-cache-buffer-and-swap-space-on-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. How to Clear Cache in Linux? Every Linux System has three options to &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/28\/how-to-clear-ram-memory-cache-buffer-and-swap-space-on-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Clear RAM Memory Cache, Buffer and Swap Space on Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-12627","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12627","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=12627"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12627\/revisions"}],"predecessor-version":[{"id":12628,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12627\/revisions\/12628"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=12627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=12627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=12627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}