If you’ve ever monitored your Linux system and seen that nearly all RAM appears “used,” you might worry you’re running out of memory. But Linux aggressively uses free memory for caching to speed up file and disk operations. In some scenarios—benchmarking, debugging, or managing memory pressure—you may want to clear RAM cache in Linux.
In this detailed guide, you will learn:
What “cache” in RAM means in Linux
When and why you might want to clear it
Safe commands to perform cache cleanup
Caveats, best practices, and how to restore swap
Frequently Asked Questions with the focus keyword
Let’s dive in.
What Does “Cache” Mean in Linux RAM?
Before you attempt to clear RAM cache in Linux, it helps to understand what that cache is and why it exists.
Linux Memory Usage: Cache, Buffers, Free Memory
Modern Linux kernels try to make best use of available RAM. Unused memory isn’t wasted—it gets used for page cache (cached file contents), dentries/inodes (filesystem metadata), and other kernel caches. These caches are held in memory but are considered “available” because the kernel can free them on demand if applications need memory.
Thus, seeing “low free memory” is often misleading: the kernel is doing its job by caching. You can read more about the kernel’s drop_caches
behavior in the official kernel documentation.
Why Clear Cache?
Here are some use cases where you might want to clear RAM cache in Linux:
Benchmarking or performance testing: To simulate “cold cache” conditions (i.e. measure disk IO without benefit of cache).
Debugging memory pressure: To see how the system behaves when caches are dropped.
Temporarily freeing memory: In extreme cases where caches are too large and affecting application performance (though this is rarely ideal).
Note: It is not generally recommended to do this on production systems regularly because the kernel’s memory management is optimized already, and dropping cache can degrade performance.
How to Clear RAM Cache in Linux Safely
This is the central section where you learn actionable commands to Clear RAM Cache in Linux.
Step 1: Sync to Flush Dirty Pages
Before dropping caches, you should flush in-memory data (dirty pages) to disk:
sudo sync
The sync
command ensures that any pending writes are committed to disk, so drop_caches
will not discard unsaved changes.
Step 2: Use drop_caches
to Clear Caches
The kernel exposes a tunable file, /proc/sys/vm/drop_caches
, which you can write to in order to drop various caches. According to official kernel docs:
1
→ drop pagecache2
→ drop dentries and inodes3
→ drop pagecache, dentries, and inodes (everything)
Here are commands to execute:
# Clear pagecache only sudo sync && echo 1 | sudo tee /proc/sys/vm/drop_caches # Clear dentries and inodes only sudo sync && echo 2 | sudo tee /proc/sys/vm/drop_caches # Clear all: pagecache + dentries + inodes sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches
Alternatively, you might see commands written like:
sudo sync; echo 3 > /proc/sys/vm/drop_caches
But the version with sudo tee
is safer with permissions.
Step 3 (Optional): Clear Swap and Reactivate
If you also want to clear swap space, you can temporarily turn off all swap and then re-enable it:
sudo swapoff -a sudo swapon -a
This forces everything from swap back into RAM or discard non-essential swap usage. Use caution—this can stress memory.
Example Full Sequence
Putting it all together:
sudo sync echo 3 | sudo tee /proc/sys/vm/drop_caches sudo swapoff -a && sudo swapon -a
After this, you can re-check memory stats with:
free -h
You’ll often see “used” memory drop—and caches shrink. But note, new caches will begin filling again as the system reads files.
When Not to Clear RAM Cache in Linux
Because Linux is good at managing memory, manually clearing cache is rarely beneficial in everyday use. Here are reasons you should avoid it:
It increases I/O and CPU overhead: dropping and reloading caches can slow things.
You lose performance benefits: cached data speeds up file reads.
It’s mostly useful for testing, not production.
As one caution from community wisdom: clearing caches repeatedly is often considered “cargo-cult system administration”—i.e. doing something that seems plausible but is generally counterproductive.
Therefore, unless you have a specific reason (benchmark, debugging, memory emergency), let the kernel manage RAM and caches.
Automating Cache Drop (Use With Caution)
You could create a cron job or scheduled task to clear RAM cache in Linux periodically. But use this only in controlled environments.
For example, to drop all caches at midnight daily:
0 0 * * * root sync && echo 3 > /proc/sys/vm/drop_caches
You might create a script (e.g. /usr/local/bin/clear_cache.sh
) with:
#!/bin/bash sync echo 3 > /proc/sys/vm/drop_caches
Then schedule it via root’s crontab. But again, do this only when you understand the tradeoffs.
Sample Output & Before / After
Here’s a hypothetical “before vs. after” of free -h
:
Before: total used free shared buff/cache available Mem: 8.0G 6.5G 500M 50M 1.0G 1.2G Swap: 2.0G 200M 1.8G
After clearing: total used free shared buff/cache available Mem: 8.0G 5.5G 1.0G 40M 1.5G 1.6G Swap: 2.0G 0G 2.0G
You’ll see drop in “used” and increase in “free” or “available” memory. But note, these gains may shrink quickly as the system caches again.
Conclusion
Knowing how to Clear RAM Cache in Linux is a useful tool in your arsenal—especially for benchmarking, debugging, or forced memory management. But it’s not something to do casually on production systems.
Always run
sync
before dropping cachesUse the
drop_caches
interface properly (withtee
or sudo)Be careful with swapoff/swapon
Understand the performance implications
Let the kernel handle things most of the time
When used correctly, cache cleanup can help you simulate cold cache conditions or diagnose memory issues—but it should not replace proper memory management or system tuning.
Frequently Asked Questions (FAQs)
Q1. How often should I clear RAM cache in Linux?
You should rarely schedule frequent cache clearing. Use it only when needed (e.g., for benchmarking or debugging). Overuse can degrade system performance.
Q2. Does clearing cache free up memory permanently?
No. It frees up memory temporarily by dropping clean caches. The kernel will rebuild cache as files get accessed again.
Q3. Is it safe to clear RAM cache in Linux on a production server?
Generally, no. Dropping cache can lead to more disk I/O, slowing performance. Use only in controlled or maintenance windows.
Q4. Will clearing cache delete files?
No. Clearing RAM cache does not delete files. It only discards in-memory caches of file content and metadata.
Q5. What exactly does echo 3 > /proc/sys/vm/drop_caches
do?
It tells the kernel to drop pagecache, dentries, and inodes—all clean, unused caches (after sync
) without affecting active data.
Suggested Read:
- How to clear var space in Linux – Best Practices for Freeing Up Disk Space
- How to Install Spaceview Disk Space Analyzer (Disk Utility) in Ubuntu – A Best Disk Usage Indicator for Linux
- Best Useful Linux DF Command With Examples
- Useful Linux Fdisk Command With Examples – A Linux Disk Partition Tool
- How to Extend Root Partition in Linux: Step-by-Step Guide for Safe Disk Expansion