Clearing PageCache, dentries and inodes in Linux
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. Use the commands below to clear PageCache, dentries and inodes, or PageCache, dentries and inodes. Keep it mind it isn't a good idea to auto clear RAM cache on production servers. However, if you are troubleshooting an issue with a runaway process or need to ensure all fetching of data comes from disk clearing cache can be very helpful.
To clear PageCache only.
To clear dentries and inodes.
Clear PageCache, dentries and inodes.
Steps to clear cache on a recurring schedule
Step 1: Create a script to clear PageCache, dentries and inodes.
The command that's probably doing the most for you is the sync command which is probably freeing memory dedicated to the buffers used for asyncronous disk I/O (as well removing dirty from all entries in the filesystem cache, making them candidates for eviction in low memory conditions). I have a problem with the cache memory in linux 2.4.20 kernel. How to flush the cache memory by external commands or by kernel configuration. Is there any way to reduce the use of cache memory. When I'm trying to mount CF card and copy one tar file and un-tar the file in the same CF card.
Step: Edit crontab to run the script every 59 minutes.
Now you may call the script whenever you required to clear ram cache. Now set a cron to clear RAM cache everyday at 2PM. Open crontab for editing. # crontab -e Append the below line, save and exit to run it at 2PM daily. 0 2. /path/to/clearcache.sh.
Verify the configuration
Using the tail command we can view the last X lines of the cron log. With the command below I am viewing just the last 14 lines and I can see when the drop_caches.sh is run and when completed. In the example below the script was run at 09:59:01 and again at 10:59:01. This will be repeated every 59 minutes per the crontab configuration.
In this article we will discuss a very important technique for boosting the performance of your Linux systems. We will flush / clear Linux system’s memory cache at a periodic periods to increase the available free memory space for other processes running on your Linux box.
Linux systems have implemented a memory management efficiently and even more than that. But many times Linux systems face a low memory issues due to either the number of running processes “heavy loaded systems” or a hungry process that eating your server’s memory, this force Linux systems to use the swap space to cache a processes data, which also decrease the system performance “If swapping is enabled”.
Cache is used to keep data to use frequently by operating system always available. Reading data from cache is much much faster than reading data from hard drive “actually this a mistake to compare reading data from RAM and reading data from hard disk”.
It’s good for OSes to get data from cache in memory and if any data not found in the cache, it reads from hard disk. So it’s no problem to flush cache memory.
Here’s we will show you how to Flush Memory Cache and swap space on Linux Server.
So, Let’s start
Flush Linux Memory Buffer Cache:
There are three options available in Linux systems to flush memory cache. We listing them in a descending order of the amount of freed memory space. Use one of below as per your requirements.
- Free pagecache, dentries and inodes in cache memory
- Free dentries and inodes use following command
- Free pagecache only use following command
sync will flush file system buffers, it forces changed blocks to disk, update the super block. Then we drop the memory caches.
Schedule Cron to Flush Cache Regularly
Its a good idea to schedule following in crontab to automatically flush cache on regular periods. Append the following cron job to your root existing cron jobs
The above cron job will be executed every three hours and flushes all cached memory on system. This will give us the maximum amount of RAM we can free. This schduale can be used for staging and development servers,but for production servers it’s not good idea to clear cache every three hours.
Java for mac mojave. For your production server’s just choose the time where only few users are connected to your server and run the above cron job once per day at that time.
Let’s assume that your server has a few /no users using it at 2 AM, so you will run the above cron job daily at 2 AM using the following command:
Now, we are save from any slowness your users can face during the emptying cache time.
Find Cache Memory uses in Linux
Use free command to find out cache memory uses by Linux system. Output of free command is like below
Sample Output
Last column is showing cached memory ( 590 MB) by system. -m option is used for showing memory details in MB’s. In our system we also use a disk swap partition, So If you want to free it also, see the following part:
How to Clear Swap Space in Linux?
If you want to clear Swap space, you may like to run the below command.
To free the swap partition just turn it off the turn it back on.
Here’s comes three important questions we must answer on them:
Is it a good idea to free Buffer and Cache in Linux that might be used by Linux Kernel?
Linux Command To Clear Memory
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.
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.
Remove Caches On Kali Linux
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.
Is it good idea to Swap space on production server?
Clean Cache And Buffer On Centos 7
No! it’s not. Never use swap space on heavily loaded production server’s. Swap space can be used in staging and development servers as they just for testing and developments purposes. Instead of using swap space increase you ram size. Swap space had important rule in old days. In those days the ram memory size was too small and too expensive so came the importance of swap space.
Now a days, The ram has huge size with moderate cost so no need to use swap space on production servers, because it decreases the server performance “actually it kills the server performance”.
Summary
In this article we explained how to clear different types of memory caches “page cache,dentries and inodes” and clear swap space. According to your system you can decide when and how to free the cache memory, You can determine which cache type you will free. Finally do not user swap space in production servers.
I hope this article is good enough for you.
See you in other articles
How To Clear Cache In Windows 10
If You Appreciate What We Do Here On Mimastech, You Should Consider:
Where Is My Cache On My Computer
- Stay Connected to: Facebook | Twitter | Google+
- Support us via PayPal Donation
- Subscribe to our email newsletters.
- Tell other sysadmins / friends about Us - Share and Like our posts and services
We are thankful for your never ending support.