Clear My Buffers! Clear My Cache! On Linux!

Getting through your buffers and cache is pretty straight forward and requires just a couple of scripts.

Emptying the buffers cache

If you ever want to empty it you can use this chain of commands.

# free && sync && echo 3 > /proc/sys/vm/drop_caches && free

             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    buffers     cached
Mem:       1018916     685008     333908          0        224     108252
-/+ buffers/cache:     576532     442384
Swap:      2064376        128    2064248

You can signal the Linux Kernel to drop various aspects of cached items by changing the numeric argument to the above command.

  • To free pagecache:

    # echo 1 > /proc/sys/vm/drop_caches
    
  • To free dentries and inodes:

    # echo 2 > /proc/sys/vm/drop_caches
    
  • To free pagecache, dentries and inodes:

    # echo 3 > /proc/sys/vm/drop_caches
    

The above are meant to be run as root. If you're trying to do them using sudo then you'll need to change the syntax slightly to something like these:

$ sudo sh -c 'echo 1 >/proc/sys/vm/drop_caches'
$ sudo sh -c 'echo 2 >/proc/sys/vm/drop_caches'
$ sudo sh -c 'echo 3 >/proc/sys/vm/drop_caches'

NOTE: There's a more esoteric version of the above command if you're into that:

$ echo "echo 1 > /proc/sys/vm/drop_caches" | sudo sh

Why the change in syntax? The /bin/echo program is running as root, because of sudo, but the shell that's redirecting echo's output to the root-only file is still running as you. Your current shell does the redirection before sudo starts.

Seeing what's in the buffers and cache

Take a look at linux-ftools if you'd like to analyze the contents of the buffers & cache. Specifically if you'd like to see what files are currently being cached.

fincore

With this tool you can see what files are being cached within a give directory.

fincore [options] files...

  --pages=false      Do not print pages
  --summarize        When comparing multiple files, print a summary report
  --only-cached      Only print stats for files that are actually in cache.

For example, /var/lib/mysql/blogindex:

root@xxxxxx:/var/lib/mysql/blogindex# fincore --pages=false --summarize --only-cached * 
stats for CLUSTER_LOG_2010_05_21.MYI: file size=93840384 , total pages=22910 , cached pages=1 , cached size=4096, cached perc=0.004365 
stats for CLUSTER_LOG_2010_05_22.MYI: file size=417792 , total pages=102 , cached pages=1 , cached size=4096, cached perc=0.980392 
stats for CLUSTER_LOG_2010_05_23.MYI: file size=826368 , total pages=201 , cached pages=1 , cached size=4096, cached perc=0.497512 
stats for CLUSTER_LOG_2010_05_24.MYI: file size=192512 , total pages=47 , cached pages=1 , cached size=4096, cached perc=2.127660 
stats for CLUSTER_LOG_2010_06_03.MYI: file size=345088 , total pages=84 , cached pages=43 , cached size=176128, cached perc=51.190476 
stats for CLUSTER_LOG_2010_06_04.MYD: file size=1478552 , total pages=360 , cached pages=97 , cached size=397312, cached perc=26.944444 
stats for CLUSTER_LOG_2010_06_04.MYI: file size=205824 , total pages=50 , cached pages=29 , cached size=118784, cached perc=58.000000 
stats for COMMENT_CONTENT_2010_06_03.MYI: file size=100051968 , total pages=24426 , cached pages=10253 , cached size=41996288, cached perc=41.975764 
stats for COMMENT_CONTENT_2010_06_04.MYD: file size=716369644 , total pages=174894 , cached pages=79821 , cached size=326946816, cached perc=45.639645 
stats for COMMENT_CONTENT_2010_06_04.MYI: file size=56832000 , total pages=13875 , cached pages=5365 , cached size=21975040, cached perc=38.666667 
stats for FEED_CONTENT_2010_06_03.MYI: file size=1001518080 , total pages=244511 , cached pages=98975 , cached size=405401600, cached perc=40.478751 
stats for FEED_CONTENT_2010_06_04.MYD: file size=9206385684 , total pages=2247652 , cached pages=1018661 , cached size=4172435456, cached perc=45.321117 
stats for FEED_CONTENT_2010_06_04.MYI: file size=638005248 , total pages=155763 , cached pages=52912 , cached size=216727552, cached perc=33.969556 
stats for FEED_CONTENT_2010_06_04.frm: file size=9840 , total pages=2 , cached pages=3 , cached size=12288, cached perc=150.000000 
stats for PERMALINK_CONTENT_2010_06_03.MYI: file size=1035290624 , total pages=252756 , cached pages=108563 , cached size=444674048, cached perc=42.951700 
stats for PERMALINK_CONTENT_2010_06_04.MYD: file size=55619712720 , total pages=13579031 , cached pages=6590322 , cached size=26993958912, cached perc=48.533080 
stats for PERMALINK_CONTENT_2010_06_04.MYI: file size=659397632 , total pages=160985 , cached pages=54304 , cached size=222429184, cached perc=33.732335 
stats for PERMALINK_CONTENT_2010_06_04.frm: file size=10156 , total pages=2 , cached pages=3 , cached size=12288, cached perc=150.000000 
---
total cached size: 32847278080

With the above output you can see that there are several *.MYD, *.MYI, and *.frm files that are currently being cached.

Swap

If you want to clear out your swap you can use the following commands.

$ free
             total       used       free     shared    buffers     cached
Mem:       7987492    7298164     689328          0      30416     457936
-/+ buffers/cache:    6809812    1177680
Swap:      5963772     609452    5354320

Then use this command to disable swap:

$ swapoff -a

You can confirm that it's now empty:

$ free
             total       used       free     shared    buffers     cached
Mem:       7987492    7777912     209580          0      39332     489864
-/+ buffers/cache:    7248716     738776
Swap:            0          0          0

And to re-enable it:

$ swapon -a

And now reconfirm with free:

$ free
             total       used       free     shared    buffers     cached
Mem:       7987492    7785572     201920          0      41556     491508
-/+ buffers/cache:    7252508     734984
Swap:      5963772          0    5963772
May 06 2020

Add or review comments

Please leave your comment

Existing comments

Comments 91


GregoryUtink
Hi, here on the forum guys advised a cool Dating site, be sure to register - you will not REGRET it [url=https://bit.ly/3fsnVVa]https://bit.ly/3fsnVVa[/url]
AndrewTrida
[url=https://freemp3.xyz/]бесплатная популярная музыка 20201[/url]
JimmyBah
[url=https://pesni-online.xyz/]слушающий музыку[/url]
JasonCam
[url=http://best-rupesni.com/]песнь 2021[/url]
Patrickvow
[url=https://allmusic.site/]музыка без остановки[/url]
ErnestByday
[url=https://kachai.xyz/]скачать новинки mp3[/url]
ArthurspeaB
[url=https://mobmusic.xyz/]музыка хиты[/url]
CedricFlons
[url=https://muzyka-mp3.xyz/]дан дан дан песня[/url]
KevinEnfof
[url=https://freepesni.site/]mp3 2021[/url]
JamesLew
[url=https://mp3top.site/]песня дана дай[/url]
KellyChisk
[url=https://findsound.xyz/]песни хороши mp3[/url]
Travissuify
[url=https://partymp3.site/]скачать песни mp3[/url]
BrooksNence
[url=https://mp3room.site/]песни онлайн бесплатно в хорошем качестве[/url]
Alfredociply
[url=https://skachat-pesni.mobi/]слушать музыку онлайн бесплатно[/url]
RalphSok
[url=https://topsounds.xyz/]музыка бесплатно регистрации[/url]
Ralphmig
[url=https://livemp3.site/]музыка[/url]
Virgilwes
[url=https://music-mp3.xyz/]мп3 бесплатно[/url]
Diegojerly
[url=https://thesounds.xyz/]песня танцующий[/url]
Derekdeavy
[url=https://mp3disc.site/]песня сердце[/url]
Thomaspaige
[url=https://skachat.site/]новая песнь[/url]
Louisedupt
[url=https://mobilemp3.top/]лучшие песни слушать бесплатно[/url]
RobertScers
[url=https://musicbank.mobi/]песни в хорошем качестве[/url]
Richardjek
[url=https://mp3ratinge.xyz/]слушать музыку бесплатно[/url]
AnthonyTal
[url=https://bestpesnya.xyz/]музыка самая самая[/url]
Charlesneede
[url=https://mp3song.top/]скачать песню ремикс[/url]
Carlosanera
[url=https://kingofmp3.top/]слушать музыку[/url]
JerrySlunc
[url=https://wowmusic.site/]песни 90[/url]
MichaelTix
[url=https://mobilemusic.top/]музыка бесплатно[/url]
Vincentdop
[url=https://mp3-skachat.mobi/]песнь 2021[/url]
DavidDuH
[url=https://mp3skachat.top/]песня танцуй[/url]
Georgedah
[url=https://muzlo.mobi/]слушать музыку онлайн без[/url]
Daniellow
[url=https://mp3mnogo.site/]песни онлайн[/url]
Dallasdraky
[url=https://musicmp3.mobi/]mp3 2021 бесплатно[/url]
Joshuamex
[url=https://million-mp3.mobi/]слушать музыку онлайн бесплатно остановки[/url]
Freddieodoky
[url=https://freesound.site/]песни скачать бесплатно mp3[/url]
MorrisCig
[url=https://yourmp3.site/]данную песню[/url]
RichardGob
[url=https://maxmp3.site/]музыку онлайн бесплатно 2021[/url]
MichaelAmuct
[url=https://mp3arhiv.xyz/]слушать песни качестве бесплатно[/url]
Jamesquags
[url=https://onlymusic.mobi/]слушать песни качестве бесплатно[/url]
Aarondig
[url=https://mp3mp3.mobi/]лучшая музыка mp3[/url]
Jamesboity
[url=https://chartmp3.top/]скачать новинки mp3[/url]
Edwardsen
[url=http://mp3storage.xyz/]песня слезы[/url]
Davidundes
[url=http://firemusic.xyz/]новинки песен 2021[/url]
AnthonyGooli
[url=https://www.sxnarod.com/index.php?showforum=13]БДСМ форум[/url]
Walterber
[url=https://www.sxn.today/index.php?showforum=8]геи[/url]
Johnnieprelt
Hi, here on the forum guys advised a cool Dating site, be sure to register - you will not REGRET it [url=http://love-angels.site/]Love-Angels[/url]
BrandonCHurb
Hi, here on the forum guys advised a cool Dating site, be sure to register - you will not REGRET it [url=http://love-angels.site/]Love-Angels[/url]
DouglasCum
[url=https://mp3fm.mobi/]яндекс музыка[/url]
Rodneytwige
[url=https://superpesni.com/]песни онлайн в хорошем качестве[/url]
NorrisSleed
[url=https://best-rupesni.com/]песнь песней mp3[/url]
LeslieAdvab
[url=https://oceanmp3.mobi/]скачать песню мп3[/url]
Jamesoffes
[url=https://320kbps.top/]музыка класс[/url]
Gordondut
[url=https://mp3bank.mobi/]музыка остановка[/url]
Jamesbeige
[url=https://free-pesni.mobi/]песня про[/url]
LeonardLew
[url=https://mp3pesnya.mobi/]песни круга[/url]
Michaelzethy
[url=https://soundfm.mobi/]музыка бесплатно без остановки[/url]
WilliamAdest
[url=https://thepesnya.com/]песня любящая[/url]
WilliamAdest
[url=https://freemp3.xyz/]качество песни[/url]
Johnnyned
[url=https://litemusic.xyz/]слушать музыку[/url]
Manuelwrism
[url=https://azermp3.com/]скачать песнь[/url]
Elliothof
[url=http://am-mus.com/]слушать русскую музыку бесплатно[/url]
Jamesnab
[url=http://kzmus.com/]песни лета 2021 года[/url]
KennethOrabs
[url=http://muskg.com/]скачать музыку без[/url]
WilliamMaito
[url=https://tjmus.com/]скачать песнь 2021[/url]
StevenMok
[url=https://mp3-tm.com/]песнь[/url]
AlfredLot
[url=https://uk-mus.com/]текст песни[/url]
JesusWam
[url=https://mp3dream.site/]песня[/url]
JerryElant
[url=https://coolmp3.site/]песня года[/url]
MarvinCix
[url=https://mp3dance.xyz/]mp3 2021[/url]
FrankFreef
[url=https://treki.site/]музыка на телефон бесплатно[/url]
ScottMox
[url=https://mp3box.site/]песня мама[/url]
JamesJulse
[url=https://topmuzyka.mobi/]детский песнь[/url]
VincentKeype
[url=https://pesnitop.xyz/]skachat mp3[/url]
CharlesSed
[url=https://music-free.online/]какую музыку[/url]
ChangExask
[url=https://muzicon.site/]песни мп3[/url]
EliasSot
[url=https://newsong.site/]слушать песни в хорошем качестве[/url]
ScottPaype
[url=https://popsong.site/]новинки песнь[/url]
Ramirovon
[url=https://thesong.site/]спой песню[/url]
JamesRax
[url=https://mp3store.site/]песни 90[/url]
GerardSmask
[url=https://mp3force.site/]песнь онлайн[/url]
HowardSinee
[url=https://ourmusic.site/]слушать песни бесплатно в хорошем качестве[/url]
Russellskiff
[url=https://topsound.mobi]музыка бесплатно[/url]
RonnieLat
[url=https://loudmp3.mobi]дали дали песня[/url]
Timothyquemn
[url=https://artmusic.mobi]слушать песни бесплатно в хорошем качестве[/url]
Henrycap
[url=https://funmusic.mobi]песни 2021[/url]
GeorgeUlcep
[url=https://bestmusic.mobi]mp3 песни в хорошем качестве[/url]
WilliamMoist
[url=https://newmp3.top]песни мп3[/url]
WilliamAreve
[url=https://justmp3.xyz]скачать mp3[/url]
Jerrynor
[url=https://newfm.xyz/]всея музыка[/url]
StevenPah
[url=https://justmusic.xyz]музыка играна[/url]
Bobbyfat
[url=https://zvuq.xyz]музыка mp3 хорошее качество[/url]