Clear Linux Buff Cache

Contents

Memory used by kernel buffers (Buffers in /proc/meminfo)

Memory used by the page cache and slabs (Cached and Reclaimable in /proc/meminfo)

Sum of buffers and cache

Estimation of how much memory is available for starting new applications, without swapping. Unlike the data provided by the cache or free fields, this field takes into account page cache and also that not all reclaimable memory slabs will be reclaimed due to items being in use (MemAvailable in /proc/meminfo, available on kernels 3.14, emulated on kernels 2.6.27+, otherwise the same as free)

Credits to stackoverflow answer.

1
sync && echo 3 > /proc/sys/vm/drop_caches

sync will flush the file system buffer, drop_cache will clean cache without killing any application/service.

  • Above echo 3 is used to Clear PageCache, dentries and inodes.
  • echo 2 to clear dentries and inodes.
  • echo 1 to clear PageCache only.

Create a script file as root user.

1
2
3
4
5
6
7
8
9
cat << 'EOF' > /path/to/clearcache.sh
#!/bin/sh

`sync && echo 3 > /proc/sys/vm/drop_caches` >/tmp/clear_cache-$(date +%d%b%Y_%H%M%S).out

cd /tmp;
find . -name 'clear_cache-*.out' -mmin +360 -delete

EOF

Set execute permission.

1
chmod +x /path/to/clearcache.sh

Create a crontab entry.

1
crontab -e

Cron job to run daily at 1 AM.

1
0  1  *  *  *  /path/to/clearcache.sh