Server Memory cache buffers reset

Linux admin does not know this, according to a conscience uptime almost all memory is used up for cache and buffers of the server, actually it is not necessary to intervene in the memory management of the system, but the system is scarce in memory, this can be prevent with a simple script.


After login as root and open the script in editor:

$ vi /usr/sbin/

# freeing cache buffers

echo $(date) >> /var/log/clearcache.log

freemem_before=$(cat /proc/meminfo | grep MemFree | tr -s ' ' | cut -d ' ' -f2) && freemem_before=$(echo "$freemem_before/1024.0" | bc)

cachedmem_before=$(cat /proc/meminfo | grep "^Cached" | tr -s ' ' | cut -d ' ' -f2) && cachedmem_before=$(echo "$cachedmem_before/1024.0" | bc)

sync; echo 3 > /proc/sys/vm/drop_caches

freemem_after=$(cat /proc/meminfo | grep MemFree | tr -s ' ' | cut -d ' ' -f2) && freemem_after=$(echo "$freemem_after/1024.0" | bc)

echo -e "This freed $(echo "$freemem_after - $freemem_before" | bc) MiB, so now you have $freemem_after MiB of free RAM." >> /var/log/clearcache.log

Now make the script executable.

chmod u+x /usr/sbin/

and run as crontask at 5, 1 3 and 9 p.m.

echo "0 5,13,21 * * root /usr/sbin/" >> /etc/crontab

Use free -m to observe free memory before and after the reset, the task is also logged under /var/log/clearcache.log and can be output to stdout with “tail /var/log/clearcache.log”.

clear cache
clear cache