Menu
in , ,

How to release unused or cached memory in Linux

Release Unused/Cached memory

To Identify cached memory or unused memory in real time by executing:

watch -n 3 free -m

watch -n 3 command will refresh free -m command outputs every 3 seconds.

output will be similar to:

and for more details about current memory usage we can executing:

watch -n 3 cat /proc/meminfo

1- Flush file system buffers by executing first using

# sync

Then  select one of the following drop_caches free:

To free page cache:

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

To free dentries and inodes:

# echo 2 > /proc/sys/vm/drop_caches

To free page cache, dentries and inodes:

# echo 3 > /proc/sys/vm/drop_caches

As we have used sync command so we can free memory without killing a process as mention in this StackOverFlow answer.

“sync” only makes dirty cache to clean cache. cache is still preserved. drop_caches doesn’t touch dirty caches and only drops clean caches. So to make all memory free, it is necessary to do sync first before drop_caches in case flushing daemons hasn’t written the changes to disk.

So commands can run as:

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

Clear Swap Space

# swapoff -a && swapon -a

Leave a Reply

Exit mobile version