{"id":5985,"date":"2016-06-30T22:49:37","date_gmt":"2016-06-30T14:49:37","guid":{"rendered":"http:\/\/rmohan.com\/?p=5985"},"modified":"2016-07-01T19:37:34","modified_gmt":"2016-07-01T11:37:34","slug":"linux-emptying-the-buffers-cache-and-swap","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=5985","title":{"rendered":"Linux | Emptying the buffers cache and Swap"},"content":{"rendered":"<p>Emptying the buffers cache<br \/>\nIf you ever want to empty it you can use this chain of commands.<\/p>\n<p>$ free &#038;&#038; sync &#038;&#038; echo 3 > \/proc\/sys\/vm\/drop_caches &#038;&#038; free<\/p>\n<p>             total       used       free     shared    buffers     cached<br \/>\nMem:       1018916     980832      38084          0      46924     355764<br \/>\n-\/+ buffers\/cache:     578144     440772<br \/>\nSwap:      2064376        128    2064248<br \/>\n             total       used       free     shared    buffers     cached<br \/>\nMem:       1018916     685008     333908          0        224     108252<br \/>\n-\/+ buffers\/cache:     576532     442384<br \/>\nSwap:      2064376        128    2064248<br \/>\nYou can signal the Linux Kernel to drop various aspects of cached items by changing the numeric argument to the above command.<\/p>\n<p>To free pagecache:<br \/>\n$ echo 1 > \/proc\/sys\/vm\/drop_caches<br \/>\nTo free dentries and inodes:<br \/>\n$ echo 2 > \/proc\/sys\/vm\/drop_caches<br \/>\nTo free pagecache, dentries and inodes:<br \/>\n$ echo 3 > \/proc\/sys\/vm\/drop_caches<br \/>\nThe above are meant to be run as root. If you\u2019re trying to do them using sudo then you\u2019ll need to change the syntax slightly to something like these:<\/p>\n<p>$ sudo sh -c &#8216;echo 1 >\/proc\/sys\/vm\/drop_caches&#8217;<br \/>\n$ sudo sh -c &#8216;echo 2 >\/proc\/sys\/vm\/drop_caches&#8217;<br \/>\n$ sudo sh -c &#8216;echo 3 >\/proc\/sys\/vm\/drop_caches&#8217;<br \/>\nNOTE: There\u2019s a more esoteric version of the above command if you\u2019re into that:<\/p>\n<p>$ echo &#8220;echo 1 > \/proc\/sys\/vm\/drop_caches&#8221; | sudo sh<br \/>\nWhy the change in syntax? The \/bin\/echo program is running as root, because of sudo, but the shell that\u2019s redirecting echo\u2019s output to the root-only file is still running as you. Your current shell does the redirection before sudo starts.<\/p>\n<p>Seeing what\u2019s in the buffers and cache<br \/>\nTake a look at linux-ftools if you\u2019d like to analyze the contents of the buffers &#038; cache. Specifically if you\u2019d like to see what files are currently being cached.<\/p>\n<p>fincore<br \/>\nWith this tool you can see what files are being cached within a give directory.<\/p>\n<p>fincore [options] files&#8230;<\/p>\n<p>  &#8211;pages=false      Do not print pages<br \/>\n  &#8211;summarize        When comparing multiple files, print a summary report<br \/>\n  &#8211;only-cached      Only print stats for files that are actually in cache.<br \/>\nFor example, \/var\/lib\/mysql\/blogindex:<\/p>\n<p>root@xxxxxx:\/var\/lib\/mysql\/blogindex# fincore &#8211;pages=false &#8211;summarize &#8211;only-cached *<br \/>\nstats for CLUSTER_LOG_2010_05_21.MYI: file size=93840384 , total pages=22910 , cached pages=1 , cached size=4096, cached perc=0.004365<br \/>\nstats for CLUSTER_LOG_2010_05_22.MYI: file size=417792 , total pages=102 , cached pages=1 , cached size=4096, cached perc=0.980392<br \/>\nstats for CLUSTER_LOG_2010_05_23.MYI: file size=826368 , total pages=201 , cached pages=1 , cached size=4096, cached perc=0.497512<br \/>\nstats for CLUSTER_LOG_2010_05_24.MYI: file size=192512 , total pages=47 , cached pages=1 , cached size=4096, cached perc=2.127660<br \/>\nstats for CLUSTER_LOG_2010_06_03.MYI: file size=345088 , total pages=84 , cached pages=43 , cached size=176128, cached perc=51.190476<br \/>\nstats for CLUSTER_LOG_2010_06_04.MYD: file size=1478552 , total pages=360 , cached pages=97 , cached size=397312, cached perc=26.944444<br \/>\nstats for CLUSTER_LOG_2010_06_04.MYI: file size=205824 , total pages=50 , cached pages=29 , cached size=118784, cached perc=58.000000<br \/>\nstats for COMMENT_CONTENT_2010_06_03.MYI: file size=100051968 , total pages=24426 , cached pages=10253 , cached size=41996288, cached perc=41.975764<br \/>\nstats for COMMENT_CONTENT_2010_06_04.MYD: file size=716369644 , total pages=174894 , cached pages=79821 , cached size=326946816, cached perc=45.639645<br \/>\nstats for COMMENT_CONTENT_2010_06_04.MYI: file size=56832000 , total pages=13875 , cached pages=5365 , cached size=21975040, cached perc=38.666667<br \/>\nstats for FEED_CONTENT_2010_06_03.MYI: file size=1001518080 , total pages=244511 , cached pages=98975 , cached size=405401600, cached perc=40.478751<br \/>\nstats for FEED_CONTENT_2010_06_04.MYD: file size=9206385684 , total pages=2247652 , cached pages=1018661 , cached size=4172435456, cached perc=45.321117<br \/>\nstats for FEED_CONTENT_2010_06_04.MYI: file size=638005248 , total pages=155763 , cached pages=52912 , cached size=216727552, cached perc=33.969556<br \/>\nstats for FEED_CONTENT_2010_06_04.frm: file size=9840 , total pages=2 , cached pages=3 , cached size=12288, cached perc=150.000000<br \/>\nstats for PERMALINK_CONTENT_2010_06_03.MYI: file size=1035290624 , total pages=252756 , cached pages=108563 , cached size=444674048, cached perc=42.951700<br \/>\nstats for PERMALINK_CONTENT_2010_06_04.MYD: file size=55619712720 , total pages=13579031 , cached pages=6590322 , cached size=26993958912, cached perc=48.533080<br \/>\nstats for PERMALINK_CONTENT_2010_06_04.MYI: file size=659397632 , total pages=160985 , cached pages=54304 , cached size=222429184, cached perc=33.732335<br \/>\nstats for PERMALINK_CONTENT_2010_06_04.frm: file size=10156 , total pages=2 , cached pages=3 , cached size=12288, cached perc=150.000000<br \/>\n&#8212;<br \/>\ntotal cached size: 32847278080<br \/>\nWith the above output you can see that there are several *.MYD, *.MYI, and *.frm files that are currently being cached.<\/p>\n<p>Swap<br \/>\nIf you want to clear out your swap you can use the following commands.<\/p>\n<p>$ free<br \/>\n             total       used       free     shared    buffers     cached<br \/>\nMem:       7987492    7298164     689328          0      30416     457936<br \/>\n-\/+ buffers\/cache:    6809812    1177680<br \/>\nSwap:      5963772     609452    5354320<br \/>\nThen use this command to disable swap:<\/p>\n<p>$ swapoff -a<br \/>\nYou can confirm that it\u2019s now empty:<\/p>\n<p>$ free<br \/>\n             total       used       free     shared    buffers     cached<br \/>\nMem:       7987492    7777912     209580          0      39332     489864<br \/>\n-\/+ buffers\/cache:    7248716     738776<br \/>\nSwap:            0          0          0<br \/>\nAnd to re-enable it:<\/p>\n<p>$ swapon -a<br \/>\nAnd now reconfirm with free:<\/p>\n<p>$ free<br \/>\n             total       used       free     shared    buffers     cached<br \/>\nMem:       7987492    7785572     201920          0      41556     491508<br \/>\n-\/+ buffers\/cache:    7252508     734984<br \/>\nSwap:      5963772          0    5963772<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Emptying the buffers cache If you ever want to empty it you can use this chain of commands.<\/p>\n<p>$ free &#038;&#038; sync &#038;&#038; echo 3 > \/proc\/sys\/vm\/drop_caches &#038;&#038; free<\/p>\n<p> total used free shared buffers cached Mem: 1018916 980832 38084 0 46924 355764 -\/+ buffers\/cache: 578144 440772 Swap: 2064376 128 2064248 total used free shared [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5985"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5985"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5985\/revisions"}],"predecessor-version":[{"id":5986,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5985\/revisions\/5986"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}