8

If I run a free -m, I get this output:

Free -m output

Is there a command that I could use to clear the used memory? I have tried this:

echo 1 > /proc/sys/vm/drop_caches

It will ask for sudo. But even after sudo, it gives bad command error.

Here is the link where I got to know about drop_caches.

I am just looking for a way on how to clear the cache.

3
  • Off topic. Belongs to superuser. Voting to close.
    – Macmade
    Sep 2 '12 at 3:33
  • Why are you asking?? It is usually not worth the effort! Sep 2 '12 at 7:42
  • I want to test the performance of an application , which initiates several processes , and generate some statistics , If i run the same application for two times the statistics will not be accurate , and also clear understanding of how the application behaves in each of scenarios , will remain as a mystery . If cache is the matter for its performance , I definitely need to know if there is any way there to clear the cache so that I can take care of it . I am really sorry , If this stack overflow is not a platform to ask such system level questions .
    – aravind.udayashankara
    Sep 2 '12 at 16:43
11

Try this as root (not sudo):

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

The problem with:

sudo echo 1 > /proc/sys/vm/drop_caches

is that the redirect happens in the initial shell - i.e. under your own account - before the "sudo echo 1" happens, which isn't the part that really needs root access. You need to get the opening of drop_caches by ">" to be inside of the sudo. One lazy way (lazy because it clones the 3 back to stdout, which you don't actually need) is:

echo 3 | sudo tee /proc/sys/vm/drop_caches

The options to write into drop_caches are:

  1. Free pagecache
  2. Free dentries and inodes
  3. Free pagecache, dentries, and inodes.

And you should sync first, so all in all:

sync ; echo 3 | sudo tee /proc/sys/vm/drop_caches

or if you don't like the spurious "3" on stdout:

sudo sh -c 'sync ; echo 3 >/prod/sys/vm/drop_caches'
1

We can make it automatically using crontab as root.

~$ sudo crontab -e

You'll see the your/new crontab file like it:

# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

And you'll add this one in new lines to flush every hour:

# Every hour flushes the memory cache on system
0 * * * * sync; echo 3 > /proc/sys/vm/drop_caches

I see in my syslog tailf output.

~$ tailf /var/log/syslog | grep 'cron'

And I see the output below:

May 31 14:07:16 debian crontab[17353]: (root) BEGIN EDIT (root)
May 31 14:07:20 debian crontab[17353]: (root) END EDIT (root)

...

May 31 15:00:02 debian CRON[22169]: (root) CMD (sync; echo 3 > /proc/sys/vm/drop_caches)
May 31 15:17:01 debian CRON[18828]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.