Clear Swap Space, RAM Memory Cache and Buffer on Linux

Clear Swap Space, RAM Memory Cache and Buffer on Linux

GNU or Linux has amazing Memory Management system for efficient Memory Management but still if you think that any process is occupying your memory then Linux offers the process to clear the RAM cache. The process to do so is given in the following text.

1. Clear Cache in Linux : You can use any of these three options and can clear Cache in Linux without interrupting any other process or service.

i. Clear Only the PageCache :
Code:
 # sync; echo 1 > /proc/sys/vm/drop_caches
Here “sync” will clear the file system buffer, “drop_cache” will clean cache without killing any other application or service and “...echo 1 > ….” will clear only the PageCache.

ii. Clear Dentries and Inodes :
Code:
 # sync; echo 2 > /proc/sys/vm/drop_caches
Here “sync” will clear the file system buffer, “drop_cache” will clean cache without killing any other application or service and “...echo 2 > ….” will clear Dentries and Inodes.

iii. Clear PageCache, Inodes and Dentries:
Code:
 # sync; echo 3 > /proc/sys/vm/drop_caches
Here “sync” will clear the file system buffer, “drop_cache” will clean cache without killing any other application or service and “...echo 2 > ….” will clear PageCache as well as Dentries and Inodes.

2. Clear Swap Space in Linux : To Clear Swap Space in Linux you should run the following command.
Code:
# swapoff -a && swapon –a
3. You can use the following command to make a Script to clear Both RAM Cache as well as Swap Space.

Code:
# echo 3 > /proc/sys/vm/drop_caches && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'
OR

Code:
$ su -c "echo 3 >'/proc/sys/vm/drop_caches' && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'" root
4. You should also use “free -h” command before and after running the above script to check the cache.
Author
bhawanisingh
Views
4,238
First release
Last update
Rating
0.00 star(s) 0 ratings
Top