Clear RAM / Cache CentOS

May 2021

Introduction

Every Linux System has three options to clear cache without interrupting any processes or services.
1. Clear PageCache only.

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

2. Clear dentries and inodes

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

3. Clear PageCache, dentries and inodes.

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

sync will flush the file system buffer. Command Separated by “;” run sequentially. The shell wait for each command to terminate before executing the next command in the sequence. As mentioned in kernel documentation, writing to drop_cache will clean cache without killing any application/service, command echo is doing the job of writing to file.
If you have to clear the disk cache, the first command is safest in enterprise and production as “…echo 1 > ….” will clear the PageCache only. It is not recommended to use third option above “…echo 3 >” in production until you know what you are doing, as it will clear PageCache, dentries and inodes.
Is it a good idea to free Buffer and Cache in Linux that might be used by Linux Kernel?
I have found that on IPHE systems you only need to do the second option sync; echo 2 > /proc/sys/vm/drop_caches. Bear in mind that this is (a) service affecting and (b) can take sometime (maybe a couple of minutes) per node.
Taken from https://www.tecmint.com/clear-ram-memory-cache-buffer-and-swap-space-on-linux/