How to Clear Cache in Linux
Tahneet Kanwal
Posted On: December 20, 2024
264 Views
8 Min Read
In Linux, cache memory, swap space, and buffers are used to speed up data access and enhance system performance. However, as these areas accumulate outdated or unnecessary data, they can start to slow down the system.
Much like clearing the cache in a browser, knowing how to clear cache in Linux is crucial for maintaining optimal performance. Regularly clearing these caches ensures that your system remains responsive and efficient.
TABLE OF CONTENTS
Steps to Clear RAM Memory Cache in Linux
Clearing RAM memory cache on Linux can help improve system performance by freeing up memory used by cached data. Most Linux systems provide three different ways to clear the cache. These methods can be used without affecting running processes or services.
Most Linux systems provide three ways to clear the cache. These methods can be used without affecting running processes or services.
Below are the three different ways:
Clear PageCache Only
The PageCache stores recently used data and files. Clearing it doesn’t affect running processes and is the safest method. To clear only the PageCache, use the following command:
Command:
1 |
sync; echo 1 > /proc/sys/vm/drop_caches |
Steps:
- Open the terminal.
- To check memory usage before clearing the cache, type:
- To clear the PageCache, type:
1 |
free |
This command shows memory usage, including swap and available memory. Run this before and after clearing the cache to see changes.
1 |
sync; echo 1 > /proc/sys/vm/drop_caches |
This will clean the PageCache and free up memory used by cached file data.
Clear Dentries and Inodes
Dentries and inodes are used by the system to manage files and directories. Clearing these entries can help when the system’s filesystem holds a large number of files in memory. To clear only dentries and inodes, use:
Command:
1 |
sync; echo 2 > /proc/sys/vm/drop_caches |
Steps:
- Open the terminal.
- Type the following command to check memory usage before clearing the cache:
1 |
free |
To clear dentries and inodes, type:
1 |
sync; echo 2 > /proc/sys/vm/drop_caches |
This will remove the cached directory and file metadata from memory.
Clear Dentries, Inodes, and PageCache
This method clears the PageCache, dentries, and inodes, freeing up the most memory. However, it’s not recommended for production environments or frequent use as it can impact system performance.
Command:
1 |
sync; echo 3 > /proc/sys/vm/drop_caches |
Steps:
- Open the terminal.
- Type the following command to check memory usage:
- To clear all caches, type:
1 |
free |
1 |
sync; echo 3 > /proc/sys/vm/drop_caches |
This will clear the PageCache, dentries, and inodes, freeing up memory.
Steps to Clear Swap Space Cache in Linux
Clearing the swap space in Linux involves turning off the swap, which moves the data from the swap partition back into RAM. This process can help free up memory and potentially improve system performance, especially when your system has been using swap space heavily.
- Turn Off Swap: To clear the swap space, disable the swap partition using the
swapoff
. This will transfer data from the swap back into RAM. - Open the terminal by pressing Ctrl + Alt + T.
- Run the following command to disable swap:
- To check memory usage, run:
- Turn Swap Back On: After clearing the swap, you can re-enable it by using the
swapon
command. This will allow the system to start using swap space again. - To re-enable the swap, type the following command:
- Verify that the swap is back on by running the following:
- 0: The system will avoid using swap as much as possible.
- 100: The system will prefer swapping over freeing up pages in memory.
- Run the command given in the terminal below.
- To clear the cache at a specific time, add the following line to the crontab file:
- To clear the swap space at the same time, add this line below the previous one:
- If using nano as the editor, press CTRL + X, then press Y to confirm saving the changes, and press Enter to finalize.
- If using vim, press ESC, type :wq, and press Enter to save and exit.
- To verify that the cron jobs have been successfully added, run the following command:
- Open the terminal on your Linux machine.
- Sync the file system to ensure all data in memory is written to disk:
- Clear the buffer cache by running this command:
Command:
Steps:
1 |
sudo swapoff -a |
Wait for at least 30 seconds to let the system finish moving data from swap to RAM.
1 |
free -m |
This will show the available memory and the amount of swap used.
Command:
1 |
sudo swapon -a |
Steps:
1 |
sudo swapon -a |
1 |
free -m |
Adjust Swappiness
Swappiness is a kernel parameter that controls how aggressively the system uses swap space. By default, it is set to 60, meaning the system will begin swapping when memory usage exceeds 60%. Adjusting this value can help you optimize memory usage based on your system’s needs.
Here are the steps to adjust swappiness:
1. View Current Swappiness Value: To check the current swappiness value, run the following command:
1 |
cat /proc/sys/vm/swappiness |
This will display the current swappiness value, which is typically set to 60 by default.
2. Change Swappiness Value: To change the swappiness value, you can use the sysctl command. The swappiness value can be set anywhere between 0 and 100:
1 |
sudo sysctl vm.swappiness=x |
Replace x with a value between 0 and 100. For example, to set swappiness to 20:
1 |
sudo sysctl vm.swappiness=20 |
This command will change the swappiness value for the current session.
3. Verify the Change: To ensure the swappiness value has been successfully changed, run:
1 |
cat /proc/sys/vm/swappiness |
This will display the new swappiness value.
Steps to Automatically Clear Cache in Linux
To automate the process of clearing memory, you can set up a cron job to run the commands at regular intervals. This way, the cache and swap space will be cleared automatically without doing it manually.
Follow these steps:
1 |
crontab -e |
This opens the crontab file for the current user in the default text editor.
1 |
30 3 * * * sudo sync; echo 3 > /proc/sys/vm/drop_caches |
This will clear the cache (including PageCache, dentries, and inodes) every day at 3:30 am.
1 |
30 3 * * * sudo swapoff -a && sudo swapon -a |
This will disable and re-enable the swap space, effectively clearing it every day at 3:30 am.
1 |
crontab -l |
This will display a list of all the cron jobs scheduled for the current user.
Now, the cache and swap space will be cleared automatically at 3:30 am every day. Adjust the time by modifying the minute and hour fields if you need a different schedule.
Steps to Clear Buffer Cache in Linux
Clearing the buffer cache in Linux can help free up memory resources used by the system for caching files and directory metadata. To clear the buffer cache in Linux, follow these steps:
1 |
sudo sync |
This command writes any pending changes to the disk to avoid data loss or corruption, but it does not clear the cache itself.
1 |
sudo sh -c 'echo 1 > /proc/sys/vm/drop_caches' |
This clears the buffer cache, releasing the memory used for storing clean data entries.
By following these steps, you can successfully clear the buffer cache in Linux and free up memory. It is especially useful if your system is running low on memory or experiencing slow performance.
Conclusion
Clearing cache for RAM, buffer, and swap space in Linux can significantly enhance system performance by freeing up resources. While it’s safe to clear these areas when needed, it may cause a brief slowdown as the system rebuilds the cache.
To streamline the process, automating cache management with cron jobs ensures your system stays efficient without manual intervention. Regularly clearing the cache in Linux helps maintain smooth performance and ensures that your system remains responsive.
Frequently Asked Questions (FAQs)
Is it safe to clear cache Linux?
Yes, clearing the cache is generally safe as it frees up space without affecting essential system data. However, clearing the cache may temporarily slow down certain processes as they rebuild the cache.
How often should I clear the cache on my Linux system?
Cache clearing frequency depends on usage. Heavy users or those who frequently run out of disk space may clear the cache monthly. Routine cache clearing can keep the system responsive.
Why do we need sudo permission to clear cache Linux?
The cache memory and buffer cache are important components of the operating system’s memory management and may also contain important data. Therefore, proper authorization is required to clear them.
How to check the RAM cache in Linux?
To check the RAM cache in Linux, you can use the following command:
free -h
Got Questions? Drop them on LambdaTest Community. Visit now