Like any other operating system, GNU/Linux has implemented memory management efficiently and even more than that. But if any process is eating away your memory and you want to clear it, Linux provides a way to flush or clear ram cache.
- Find Top 15 Processes by Memory Usage in Linux
- Find Top Running Processes by Highest Memory and CPU Usage in Linux
- How to Limit Time and Memory Usage of Processes in Linux
How to Clear Cache in Linux?
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_caches
Explanation of the above command.
sync will flush the file system buffer. Command Separated by
“;” run sequentially. The shell waits for each command to terminate before executing the next command in the sequence. As mentioned in the 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 the 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?
Free Buffer and Cache in Linux
When you are applying various settings and want to check, if it is actually implemented specially on the I/O-extensive benchmark, then you may need to clear the buffer cache. You can drop cache as explained above without rebooting the System i.e., no downtime required.
Linux is designed in such a way that it looks into the disk cache before looking onto the disk. If it finds the resource in the cache, then the request doesn’t reach the disk. If we clean the cache, the disk cache will be less useful as the OS will look for the resource on the disk.
Moreover, it will also slow the system for a few seconds while the cache is cleaned and every resource required by OS is loaded again in the disk cache.
Now we will be creating a shell script to auto clear RAM cache daily at 2 am via a cron scheduler task. Create a shell script clearcache.sh and add the following lines.
#!/bin/bash # Note, we are using "echo 3", but it is not recommended in production instead use "echo 1" echo "echo 3 > /proc/sys/vm/drop_caches"
Set execute permission on the clearcache.sh file.
# chmod 755 clearcache.sh
Now you may call the script whenever you are required to clear the ram cache.
Now set a cron to clear RAM cache every day at 2 am. Open crontab for editing.
# crontab -e
Append the below line, save and exit to run it at 2 am daily.
0 2 * * * /path/to/clearcache.sh
For more details on how to cron a job, you may like to check our article on 11 Cron Scheduling Jobs.
Is it a good idea to auto clear the RAM cache on the production server?
Clear RAM Cache on Linux Production Server?
No! it is not. Think of a situation when you have scheduled the script to clear ram cache every day at 2 am. Every day at 2 am the script is executed and it flushes your RAM cache. One day for whatsoever reason may be more than expected users are online on your website and seeking resources from your server.
At the same time, the scheduled script runs and clears everything in the cache. Now all the users are fetching data from the disk. It will result in a server crash and corrupt the database. So clear ram-cache only when required, and known your footsteps, else you are a Cargo Cult System Administrator.
How to Clear Swap Space in Linux?
If you want to clear Swap space, you may like to run the below command.
# swapoff -a && swapon -a
Also, you may add the above command to a cron script above, after understanding all the associated risks.
Now we will be combining both above commands into one single command to make a proper script to clear RAM Cache and Swap Space.
# echo 3 > /proc/sys/vm/drop_caches && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared' OR $ su -c "echo 3 >'/proc/sys/vm/drop_caches' && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'" root
After testing both the above commands, we will run the command “free -h” before and after running the script and will check the cache.
That’s all for now, if you liked the article, don’t forget to provide us with your valuable feedback in the comments to let us know, what you think is a good idea to clear ram cache and buffer in production and Enterprise?
106 thoughts on “How to Clear RAM Memory Cache, Buffer and Swap Space on Linux”
I want to ask? is it safe to clean cached memory on a Linux mail server, will this process interfere with sending/receiving emails on the user?
Yes, it is safe to remove cached memory buffer to free up memory space…
The reason to drop caches like this is for benchmarking disk performance and is the only reason it exists.
When running an I/O-intensive benchmark, you want to be sure that the various settings you try are all actually doing disk I/O, so Linux allows you to drop caches rather than do a full reboot.
What is this with echo? echo “echo 3 > /proc/sys/vm/drop_caches“. You should just use echo 3 > /proc/sys/vm/drop_caches, or echo 3 | sudo tee /proc/sys/vm/drop_caches.
The remark that ‘3’ should not be used in production systems is ridiculous and invalid. It is fully supported and stable for decades. drop_caches is usually only useful when doing benchmarks and timing tests of file systems and block devices, or network attached storage or file systems. You shouldn’t use any drop_cache in the first place in any real production system. It is only for testing and debugging.
echoing the echoed echo, echo.
Exactly, Witold. I second you. The author of this article “heard the song but got it wrong”.
It may be necessary to go back to normal operation after this settings. See the following document from kernel.org.
Returning system back to normal requires user to write “4” into drop_caches
Zeki, no there is no need to write 4. Documentation says it clearly. 4 (bit 2), is only to disable the dmesg / kernel log messages when the drop_caches is issued. writing 1, 2 or 3 is one time thing, and immediately after (or even during) the page cache and other caches will start to be populated back according to system usage.
I hardly see to ever need to use 4. It might be useful if you do it like every few minutes for some strange reasons, and want to avoid polluting the kernel log (dmesg) by spam of it. But it is just one line, and if you do it sporadically, there is zero reason to use 4.
You can’t use “echo 4” to release memories, because centos system just has 1, 2, or 3 options.
This is on a personal device that I’m only running one app on that’s not critical but want to make sure the performance is as good as it can be.
The command to clear the caches doesn’t run until I add sudo. I get access denied without it. Would this be proper?
sync; echo 3 > sudo /proc/sys/vm/drop_caches
Sometimes you need superuser privileges to run commands..Nothing to worry..
What I’m asking is WHERE I put the sudo command because it didn’t work in front of sync or echo and I didn’t realize drop-caches was an executable command that would respond to sudo. It seems to work there but I’m just making sure that’s a proper use of sudo in that syntax.
Actually found something that seems to work much better than what I last tried:
sync; sudo sh -c "echo 3 > /proc/sys/vm/drop_caches"
The buff/cache column on
free -hdropped from 340MB to 97MB and the free column went from 78MB to 217MB. Bigger difference than before.
su -c "echo 3 >'/proc/sys/vm/drop_caches' && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'" root
Is there a password for this after hitting enter ??? if not why does it ask me?
and could this be applied for the home desktops too ???
You might want to correct
When are we going to stop talking about cron, and start talking about systemd timers? It’s time, guys.
Well, not everybody is in love with systemd.
If you are in love with systemd OK go ahead and use timers but don’t pretend that every body is agree with you.
Exactly! I literally went out of my way to comment specifically on this. Why does systemd even need a timer? It’s _supposed_ to be an init system.
When are we going to stop talking about systemd and move to openRC? It’s time, guys. Long, long past time in fact.
What is the tool you used to generate the Screenshot Gif
To generate screenshot animated GIFs, have a look at ‘”peek”, available from the GitHub repository.