- How to Flush Memory Cache and Clear Swap Space on Linux Systems
- Flush Linux Memory Buffer Cache:
- Schedule Cron to Flush Cache Regularly
- Find Cache Memory uses in Linux
- How to Clear Swap Space in Linux?
- Is it a good idea to free Buffer and Cache in Linux that might be used by Linux Kernel?
- Is it good idea to auto clear RAM cache on production server?
- Is it good idea to Swap space on production server?
- Summary
- How to Clear RAM Memory Cache, Buffer and Swap Space on Linux
- To Clear Cache in Linux:
- How to Clear RAM Memory Cache, Buffer and Swap Space on Linux
- How to Clear Cache in Linux?
- Free Buffer and Cache in Linux
- Clear RAM Cache on Linux Production Server?
- How to Clear Swap Space in Linux?
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- How to clear swap memory in Linux
- More Linux resources
How to Flush Memory Cache and Clear Swap Space on Linux Systems
Posted by: Mohammed Semari | Published: August 13, 2016| Updated: August 13, 2016
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.
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.
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?
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.
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?
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 RAM Memory Cache, Buffer and Swap Space on Linux
In this article, we will see How to Clear RAM Memory Cache, Buffer, and Swap Space on Linux. In every system we do come across caches that have unwanted files and can harm our system, the same thing happens in Linux cache and if you want to clear the cache and free some memory then Linux has many commands to do that.
To Clear Cache in Linux:
In all the Linux systems we have three options to clear cache without interrupting any services or processes.
Example 1: To Clear PageCache only
Syntax :
The command # free -h will give us the status of the memory
drop_caches is used a clean cache without killing any application, you can run the # free -h command to see the difference between used and free memory before and after clearing the cache
Example 2: To Clear dentries and inodes
Syntax:
Example 3: To Clear PageCache, dentries and inodes
Syntax:
Now using Linux Kernel, to free Buffer and Cache in Linux we will Create a shell script to auto clear RAM cache daily, through a cron scheduler task., the command vim script.sh is used to create a shell script “script.sh”
Now in script, you have to add the below syntax:
Now to set run permission, to clear ram cache, you have to call the script whenever required, setting a cron to clear RAM caches every day for 3 hours.
Example 4: To Clear Swap Space in Linux
You can clear the swap space by running the below command
Syntax :
You can run the # free -h command to see the difference between used and free memory before and after clearing the swap space
Add the above command to a cron script, Here we are going to combine this two different command into one single command, to form a proper script which will help us to clear Swap Space and RAM Cache.
echo 3 > /proc/sys/vm/drop_caches & & swapoff -a & & swapon -a & & printf ‘\n%s\n’ ‘ ‘ Ram-cache and the swap get cleared’
Now Ram cache and swap will be cleared you can run # free -h command to see
After running the command you will get output like this
Источник
How to Clear RAM Memory Cache, Buffer and Swap Space on Linux
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.
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.
2. Clear dentries and inodes.
3. Clear pagecache, dentries, and inodes.
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.
Set execute permission on the clearcache.sh file.
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.
Append the below line, save and exit to run it at 2 am daily.
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.
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.
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?
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
How to clear swap memory in Linux
Photo by Kaboompics .com from Pexels
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.
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.
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.
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:
Now, you can see that my system has a swappiness value of 30. To alter the value, you want to use the following:
To verify the value that you set, simply cat the swappiness file that you looked at earlier to find out the original value. Easy day. Now that you understand the underlying parameters that control the swap behavior on our system, you’re ready to learn how to clear that memory, should the situation arise. For the first time in your terminal life, things are going to be easy here.
To clear the swap memory on your system, you simply need to cycle off the swap. This moves all data from swap memory back into RAM. It also means that you need to be sure you have the RAM to support this operation. An easy way to do this is to run ‘free -m’ to see what is being used in swap and in RAM. Once you power it off, you can wait an arbitrary amount of time (30 sec or so) to give the operation time to complete, then power the swap back on. This clears the swap memory cache and re-enables it. Here are all of the commands you’ll need!
Hopefully, this quick tip helps you clear your system swap memory if you ever find yourself in need of just such a fix.
For more information on swap space see my article outlining the basics here.
Want to try out Red Hat Enterprise Linux? Download it now for free.
Источник