How to Clear Cache in Linux

Tahneet Kanwal

Posted On: December 20, 2024

view count264 Views

Read time8 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.

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:

Steps:

  1. Open the terminal.
  2. To check memory usage before clearing the cache, type:
  3. This command shows memory usage, including swap and available memory. Run this before and after clearing the cache to see changes.

  4. To clear the PageCache, type:
  5. 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:

Steps:

  1. Open the terminal.
  2. Type the following command to check memory usage before clearing the cache:
  3. To clear dentries and inodes, type:

    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:

Steps:

  1. Open the terminal.
  2. Type the following command to check memory usage:
  3. To clear all caches, type:
  4. 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.

  1. 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.
  2. Command:

    Steps:

    1. Open the terminal by pressing Ctrl + Alt + T.
    2. Run the following command to disable swap:
    3. Wait for at least 30 seconds to let the system finish moving data from swap to RAM.

    4. To check memory usage, run:
    5. This will show the available memory and the amount of swap used.

  3. 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.
  4. Command:

    Steps:

    1. To re-enable the swap, type the following command:
    2. Verify that the swap is back on by running the following:
    3. kernel parameter

    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:

    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:

    • 0: The system will avoid using swap as much as possible.
    • 100: The system will prefer swapping over freeing up pages in memory.

    Replace x with a value between 0 and 100. For example, to set swappiness to 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:

    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. Run the command given in the terminal below.
    2. This opens the crontab file for the current user in the default text editor.

    3. To clear the cache at a specific time, add the following line to the crontab file:
    4. This will clear the cache (including PageCache, dentries, and inodes) every day at 3:30 am.

    5. To clear the swap space at the same time, add this line below the previous one:
    6. This will disable and re-enable the swap space, effectively clearing it every day at 3:30 am.

    7. If using nano as the editor, press CTRL + X, then press Y to confirm saving the changes, and press Enter to finalize.
    8. If using vim, press ESC, type :wq, and press Enter to save and exit.
    9. To verify that the cron jobs have been successfully added, run the following command:
    10. 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. Open the terminal on your Linux machine.
    2. Sync the file system to ensure all data in memory is written to disk:
    3. This command writes any pending changes to the disk to avoid data loss or corruption, but it does not clear the cache itself.

    4. Clear the buffer cache by running this command:
    5. 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

Author Profile Author Profile Author Profile

Author’s Profile

Tahneet Kanwal

Tahneet Kanwal is a software engineer with a passion for frontend development and a keen eye for user experience. With a strong foundation in web technologies, she brings a practical perspective to her writing. As a technical content writer at LambdaTest, Tahneet combines her engineering expertise with her communication skills to craft engaging, SEO-friendly articles. Her work spans various topics in web development, software testing, and emerging tech trends, keeping readers informed and inspired.

Blogs: 22



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free