How to clear cache on Linux?

In my daily work, sometimes I will encounter the issue that Linux OS run out of memory and this will definitely slow down my work especially when I am running a memory consuming application.

Below are the steps how to fix it,

1. from a new session, copy and run below in the shell,

while true
sync; echo 3 > /proc/sys/vm/drop_caches
free -m
echo ".... Will execute in next 1Min..."
sleep 60

As this is a non-destructive operation, and dirty objects are not free-able, here I run “sync” first in order to make sure all cached objects are freed.

And sleep 60 is good enough for my case.

echo 3 means free pagecache, dentries and inodes.

More details,