How to clear cached memory in Debian?
Try this as root
(not sudo):
#sync && echo 3 > /proc/sys/vm/drop_caches
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:
- Free pagecache
- Free dentries and inodes
- 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'
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)