Hero Background

Power Your Software Testing with AI Agents and Cloud

The Native AI-Agentic Cloud Platform to Supercharge Quality Engineering. Test Intelligently and Ship Faster.

Browsers and OS

How to Clear Cache in Linux

Learn how to clear cache in Linux, including RAM, buffer, and swap space, to boost system performance and keep your system responsive with simple commands.

Last Updated on:

Linux tracks memory use in the free command's output, and running sync followed by echo 1, 2, or 3 into /proc/sys/vm/drop_caches clears the PageCache, dentries and inodes, or all three without harming running processes. The vm.swappiness kernel parameter, set to 60 by default, can also be lowered through sysctl to make the system favor RAM over swap.

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:

sync; echo 1 > /proc/sys/vm/drop_caches

Steps:

  • Open the terminal.
  • To check memory usage before clearing the cache, type:
  • free
    

    This command shows memory usage, including swap and available memory. Run this before and after clearing the cache to see changes.

  • To clear the PageCache, type:
  • 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:

sync; echo 2 > /proc/sys/vm/drop_caches

Steps:

  • Open the terminal.
  • Type the following command to check memory usage before clearing the cache:
  • free

To clear dentries and inodes, type:

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:

sync; echo 3 > /proc/sys/vm/drop_caches

Steps:

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

Command:

Steps:

    • Open the terminal by pressing Ctrl + Alt + T.
    • Run the following command to disable swap:
    • sudo swapoff -a 
    • 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.

Command:

    sudo swapon -a
    

Steps:

    • To re-enable the swap, type the following command:
    • Verify that the swap is back on by running the following:

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:

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:

  • 0: The system will avoid using swap as much as possible.
  • 100: The system will prefer swapping over freeing up pages in memory.
sudo sysctl vm.swappiness=x

Replace x with a value between 0 and 100. For example, to set swappiness to 20:

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:

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:

  • 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:

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:

  • 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:

Can an AI Agent Run the drop_caches Commands in This Guide?

Often no. An AI coding agent running shell commands inside a Docker or containerd sandbox cannot write to drop_caches, because the runtime mounts /proc/sys read-only by default.

  • Read-only /proc/sys: runc, the reference runtime behind Docker and containerd, lists /proc/sys among its default read-only paths, so drop_caches stays unwritable even when the agent's process runs as root inside the container.
  • Host-wide blast radius: drop_caches is not scoped to one container. Letting any sandbox flush it would drop the page cache for every other container sharing that kernel, which is why the path is locked down by default.
  • The scoped alternative: cgroup v2 adds a memory.reclaim file, introduced in Linux kernel 5.19, that frees memory from the calling process's own cgroup only. An agent inside a properly configured container can use it without host-wide access.
  • When to hand off: an agent that hits Permission denied on drop_caches should report the failure rather than retry with sudo, since a typical container has no sudo binary or root password to escalate with.

The RAM, swap, and buffer commands earlier in this guide still work as written on a full virtual machine or a bare-metal host, where the process owns the entire kernel. See the cgroup v2 documentation for the memory.reclaim syntax.

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.

sudo swapoff -a
free -m

Adjust Swappiness

Steps to Automatically Clear Cache in Linux

crontab -e
30 3 * * * sudo sync; echo 3 > /proc/sys/vm/drop_caches
30 3 * * * sudo swapoff -a && sudo swapon -a
crontab -l

Steps to Clear Buffer Cache in Linux

sudo sync
sudo sh -c 'echo 1 > /proc/sys/vm/drop_caches'

Conclusion

Author

...

Tahneet Kanwal

Blogs: 29

  • Twitter
  • Linkedin

Tahneet Kanwal is a freelance technical content writer with over 2 years of hands-on experience in frontend development and technical writing. She holds a B.Tech in Information Technology from University College of Engineering and Technology (UCET). Tahneet creates clear, SEO-optimized content on web technologies, software testing, and automation tools, leveraging her skills in HTML, CSS, JavaScript, React, Tailwind CSS, and various tools like VS Code, GitHub, Figma, and Canva. She is the author of 30+ technical blogs and an open-source contributor through Hacktoberfest. She has also participated in the Google Cloud Arcade Facilitator Program and holds certifications as a Meta Android Developer (Coursera) and in Web Development (Internshala). Over time, she has evolved her writing to prioritize structure, readability, and SEO while maintaining technical depth.

Reviewer

...

Shubham Suri

Reviewer

  • Linkedin

Shubham Suri is a Lead Member of Technical Staff at TestMu AI (formerly LambdaTest), building the frontend and web interfaces of the quality engineering platform. He works across React.js, HTML5, CSS3, Bootstrap, jQuery, JavaScript, and Angular to create and maintain webpages and web applications. He brings around a decade of experience in web and frontend development, with earlier roles as a Software Engineer at Hard Shell Technologies and an IT Analyst at Cyrus Group. Shubham holds a B.Tech in Computer Science from Uttar Pradesh Technical University.

Add to Google preferred sources

Summarise with AI

Copied to Clipboard!
...

3000+ Browsers. One Platform.

See exactly how your site performs everywhere.

Try it free
...

Write Tests in Plain English with KaneAI

Create, debug, and evolve tests using natural language.

Try for free

Clear Cache in Linux FAQs

Did you find this page helpful?

More Related Blogs

TestMu AI forEnterprise

Get access to solutions built on Enterprise
grade security, privacy, & compliance

  • Advanced access controls
  • Advanced data retention rules
  • Advanced Local Testing
  • Premium Support options
  • Early access to beta features
  • Private Slack Channel
  • Unlimited Manual Accessibility DevTools Tests