How To Clear The Cache In Linux
Clear cache Linux
In this tutorial, we will learn how to clear the RAM Cache in Linux and clean Swap space.
Just like all other operating systems, GNU/ Linux also has implemented a memory management system. Although, Linux's memory management is much efficient and even much used than other operating systems. However, in some cases where you want to control memory management or if there are one or more processes that are eating up your system's RAM. In such case, we want to stop it; Linux provides few ways to flush (clear ram cache).
How to Clear Cache in Linux?
Almost in every Linux system, there are three methods provided to clear the cache. Some of these can be used remarkably without interrupting any ongoing processes or services. The three ways are as follows:
- Clear PageCache only
- Clear dentries and inodes
- Clear dentries and inodes, Clear PageCache
We will discuss all three methods in details and see their practical implementation as well:
1. Clear PageCache only-
This method is one of the safest ways to clean the cache of Linux because it can clear the cache without killing any ongoing operations, applications, and services. This is said to be the safest way to clean the cache as it can be used in production, and it only cleans the PageCache. In this method, the following commands are used:
Command
Explanation:
In the above command, the "sync" is used for flushing the file system buffer, and the command is separated using ";" executed in a sequential manner.
However, the shell waits for every command to be executed or terminated before beginning the execution next command available in the sequence.
The drop_caches are used for flushing or cleaning the cache without killing or affecting any other running application or services, as it is also clearly mentioned in the Kernel Documentation. The "echo" command does the job of writing to files.
Steps to Clear the PageCache using Terminal:
Step.1 - Open the Terminal in Linux and type the following given command, and hit enter. Once the command gets executed, it will display the information about the system's memory, as shown in the example:
Command
The above command is used to display how much memory is used and display other factors like total available memory, swap memory, etc. We can use this "free" command just before and after while using each method.
It is necessary to use the "free" command before and after executing the actual command so that we can easily see or detect changes in memory allocation. Let see an example to understand how the whole process actually works:
Example
2. Clear dentries and inodes-
This method is used to clear the memory of dentries and inodes. It is just like the first method but slightly different as it is used to clear the memory of dentries and inodes both instead of clearing the memory of PageCache. Still, it is much like the first method that we have discussed above. The command used in this method is given below:
Command
Lets us see an example to understand how to implement this method:
Example
3. Clear dentries and inodes, Clear PageCache
It is one of the most effective methods. Nevertheless, it is not traditionally recommended to be used until you know what you are doing because it is not considered the safest to use, especially in production. The command used in this method uses the "echo 3 >" which cleans the PageCache, dentries, and inodes at the same time.
Command:
To understand in more details how it actually works, consider the given example:
Example:
Note: It is not recommended to use it in production unless you know what you are doing because it will instantly clean PageCache and dentries, inodes too, as we already discussed above.
Swap space
In general, the swap space is used when the utilization of the Physical Memory is almost at its peak. So when and if the system needs more memory resources and the physical ram gets full, all the inactive pages are transferred to the swap memory space.
However, the swap memory space also helps the system as a small but effective amount of Ram. Still, undoubtedly it is not recommended to consider the Swap Space as the Physical Ram.
How to clear swap space in Linux?
To clear the swap memory on your system, you simply need to cycle off the Swap. This will move all data from swap memory back into RAM. It also means that you need to be sure that you have enough RAM in your system to support this operation.
An easy way to do this is to run the 'free-m' command to see what is being used in the Swap, RAM, and then disable the Swap.
Once you turn it off, you should wait 30 seconds or more to allow some time for the operation to complete, and then turn the Swap back on. This clears the swap memory cache and re-enables it, but before clearing the Swap, you should see your system's current (or default) setting.
To see the current setting of your system's swappiness, you just need to open the terminal use the following command as shown in the output:
Output
As you can see, our system's current Swappiness value is "60" seconds. However, if you want to change it, you can set it anywhere between 0 and 100. The process of editing (updating) this value is quite simple. All, you need to type the following command in the Terminal and press enter:
Command:
(Here x is the swap value that we want to set)
As you can see in the above-given output, our system's current swap value (or Default value) is 60. Let us try to change the swap value to 40.
1. Open the Terminal by Pressing the Ctrl+Alt+T on the Keyboard.
2. Once the terminal gets open, type the following command and press enter, as shown in the image given below:
Output
Note: It may ask you to enter the Password as we need to run this command with the superUser (root user) privileges.
Verification
To verify that the swap value is successfully updated, type the following command and hit enter button:
Output
As you can see in the output, we have successfully updated the swap value to 40.
Clear Swap Space
To clear the swap space, the process is quite simple because we simply need to cycle off the Swap, due to which all the data or inactive pages are transferred back into the RAM from the Swap memory space. Once you cycle off the Swap, you must wait for 30 seconds and need to restart or cycle on the swap space again.
Useful Commands:
These are some basic commands that will help us in cleaning the Swap Space:
Steps to clear Swap Memory
Let us see how to use these commands to clear swap space or memory:
Step1. Open the Terminal by pressing the "Ctrl+Alt+ T " on the keyboard.
Step2. Once the Terminal gets open, type the "sudo -i" command because we may require superuser privileges to run the above-given commands:
Step3. Type the "free -m" command to view the utilization of memory before cycle off the Swap:
Step - 4 Now, type the "Swapoff -a" command and press enter to cycle off the Swap as shown below:
Step5. After the cycle off, the Swap waits for 40 seconds at least.
Step6. Now type "swap-a" and press enter to re-enable Swap. To see the difference (or change) in memory usage, run the "free-m" command one more time, as shown below:
Hopefully, this will help you clear your system's swap memory if you ever find yourself in such a situation.
How To Clear The Cache In Linux
Source: https://www.javatpoint.com/clear-cache-linux
Posted by: groveloung1959.blogspot.com
0 Response to "How To Clear The Cache In Linux"
Post a Comment