The memory used by the page cache is available to be given to any process if the kernel decides to do that. The memory has not been leaked. If you want to do an experiment, write a program which repeatedly writes to every page in a 4 or 6 gigabyte space. Page Cache The page cache could contain any memory mappings to blocks on disk. That could conceivably be buffered I/O, memory-mapped files, paged areas of executables–anything that the OS could hold in memory from a file. Jan 31, 2019 First option to clear memory caches on Linux. Command to free pagecache, dentries and inodes in cache memory in Linux. Sync; echo 3 /proc/sys/vm/dropcaches Second option to clear memory cache.
Clear Ram Cache Linux Server
More Linux resources
Swap memory is usually a 'set it and forget it' type of affair. Most enterprise environments have swap built into the systems, and these memory caches are not manipulated unless there is an apparent lack of memory available or if a server crashes due to the OOM killer (out of memory) error. However, there is a niche situation that can cause an administrator to need to clear the system swap manually. If that is the situation that you find yourself in, you’ve come to the right place. This article is a discussion about this situation and the solution required.
Feeling Swappy?
Occasionally, a system uses a high percentage of swap memory even when there is RAM available for use. The culprit here is the ‘swappiness’ of the system. Yep, you read that right...swappiness. So now that you know the lingo, you're ready to explore what it means. Swappiness refers to the kernel parameter responsible for how much and how often that the system moves data from RAM to swap memory.
Clear RAM Memory Cache, Buffer and Swap Space When you write data, it doesn’t necessarily get written to disk right then. The kernel maintains caches of many things, and disk data is something where a lot of work is done to keep everything fast and efficient.
The default value for swappiness
is 60; however, you can manually set it anywhere between 0-100. Small values cause little swapping to occur, whereas high values can cause very aggressive swapping. A value of zero causes no swapping at all to occur, so if you want to minimize swapping to its lowest possible value without turning it off, you should set it to at least one.
[ Free download: Advanced Linux commands cheat sheet. ]
If you wanted to change up the swappiness of your system, the procedure is very straight-forward. You can check your current swappiness setting by running the following command:
It should look something like this:
Like any other operating system, GNU/Linux has implemented a 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.
Clear RAM Cache and Swap in Linux
How to Clear Cache in Linux?
Every Linux System has three options to clear cache without interrupting any processes or services.
- Clear PageCache only.
- Clear dentries and inodes.
- Clear PageCache, dentries and inodes.
Explanation of above command.
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?
When you are applying various settings and want to check, if it is actually implemented specially on I/O-extensive benchmark, then you may need to clear 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 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 2am via a cron scheduler task. Create a shell script clearcache.sh and add the following lines.
Set execute permission on the clearcache.sh file.
Now you may call the script whenever you required to clear ram cache.
Now set a cron to clear RAM cache everyday at 2am. Open crontab for editing.
Append the below line, save and exit to run it at 2am daily.
For more details on how to cron a job you may like to check our article on 11 Cron Scheduling Jobs.
Is it good idea to auto clear RAM cache on production server?
No! it is not. Think of a situation when you have scheduled the script to clear ram cache everyday at 2am. Everyday at 2am 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 resource from your server.
At the same time scheduled script run and clears everything in cache. Now all the user are fetching data from disk. It will result in server crash and corrupt the database. So clear ram-cache only when required,and known your foot steps, 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.
Also you may add above command to a cron script above, after understanding all the associated risk.
Now we will be combining both above commands into one single command to make a proper script to clear RAM Cache and Swap Space.
OR
After testing both above command, we will run command “free -h” before and after running the script and will check cache.
How to Clear Swap Space in Linux?
If you want to clear Swap space, you may like to run the below command.
Also you may add above command to a cron script above, after understanding all the associated risk.
Now we will be combining both above commands into one single command to make a proper script to clear RAM Cache and Swap Space.
OR
After testing both above command, we will run command “free -h” before and after running the script and will check cache.
References:
How To Clear Ram Memory
Clear Ram Cache Linux
1.http://www.tecmint.com/clear-ram-memory-cache-buffer-and-swap-space-on-linux/