Clear RAM Cache and Swap on Linux
In this article
Clearing RAM cache, buffer, and swap space on Linux helps free up memory used by cached files and inactive pages, potentially improving system responsiveness under memory pressure. This is useful after memory-intensive tasks or when troubleshooting performance issues. While Linux manages memory efficiently on its own, manual clearing may be needed in specific scenarios like benchmarking, testing, or preparing a system for a critical task. This guide explains how to safely flush caches and clear swap space without harming your system.
What You'll Need
- Access to a Linux terminal with root or sudo privileges
- Basic understanding of Linux command line
- A running Linux system (any major distribution)
- Optional: familiarity with system monitoring tools like
free,htop, orvmstat
Step-by-Step
- Open a terminal on your Linux system. You can do this via Ctrl+Alt+T on most desktop environments or through SSH if accessing remotely.
- Check current memory usage before making changes. Run:
free -h
This displays total, used, free, shared, buff/cache, and available memory. Note the values under "Mem" and "Swap" for comparison later. - Sync filesystems to ensure all pending writes are committed to disk. Run:
sync
This prevents data loss when clearing caches and ensures consistency. - Clear pagecache, dentries, and inodes by writing to the
drop_cachessysctl interface. Run:echo 3 | sudo tee /proc/sys/vm/drop_caches
This command clears three types of cache:- Pagecache (cached file data)
- dentries (directory entries)
- inodes (file metadata)
- Verify cache reduction by checking memory again:
free -h
You should see an increase in free memory under "buff/cache" and a decrease in used memory. - Turn off swap space to clear its contents. Run:
sudo swapoff -a
This moves all data from swap back into RAM. Ensure you have enough free RAM before this step—otherwise, the system may become unresponsive. - Turn swap back on to re-enable it:
sudo swapon -a
This reloads swap space, now empty. You can verify withfree -h—swap usage should be near zero. - Optional: Clear only specific caches. Instead of
echo 3, use:echo 1to clear only pagecacheecho 2to clear dentries and inodes only
Common Mistakes
- Running
drop_cacheswithout syncing: Skippingsyncrisks data loss if pending writes haven’t been flushed to disk. - Disabling swap without enough RAM: If physical memory is insufficient to hold swapped data,
swapoff -acan cause system freezes or crashes. - Expecting performance gains on idle systems: Clearing caches on a system with plenty of memory may slow down subsequent file operations, as cached data must be reloaded from disk.
- Running commands without sudo: Most commands require root privileges. Forgetting
sudoresults in "Permission denied" errors. - Confusing cache with application memory:
drop_cachesdoes not kill processes or free memory they’re actively using—only kernel-managed caches.
Tips
- Monitor memory before and after: Use
free -horhtopto observe changes and confirm the operation worked. - Use in scripts cautiously: Automating cache clearing can interfere with system performance. Only use in controlled environments like testing or benchmarking.
- Consider system load: Avoid clearing caches during high I/O activity to prevent temporary slowdowns.
- Use
vm.drop_cachestemporarily: The change is not persistent across reboots. This is intentional—Linux is designed to use free memory for caching. - Combine with log rotation: If freeing disk space is also a goal, consider cleaning logs or temporary files after clearing memory.
Troubleshooting
- "Permission denied" when writing to
/proc/sys/vm/drop_caches: Ensure you’re usingsudo. The correct full command isecho 3 | sudo tee /proc/sys/vm/drop_caches. Using justsudo echo 3 > /proc/sys/vm/drop_cachesfails due to shell redirection permissions. - System becomes slow after clearing cache: This is expected. Cached data must be reloaded from disk. The system will naturally rebuild caches as files are accessed.
- Cannot turn off swap: "swapoff: Cannot allocate memory": This usually means insufficient RAM to hold swapped data. Free up memory by closing applications or reboot the system instead.
- Swap usage remains high after
swapon -a: The command re-enables swap, but if processes are still memory-heavy, they may immediately start using it again. Check withtoporhtop. - Changes don’t persist after reboot: This is normal. Cache and swap states are reset on boot. No action is needed unless you want to automate the process (not recommended for production systems).
Frequently Asked Questions
Does clearing RAM cache damage my system?
No. It only removes temporary cached data that Linux can regenerate. It does not affect files, applications, or settings.Will clearing cache speed up my computer?
Not necessarily. On systems with sufficient RAM, caching improves performance. Clearing it may temporarily slow down file access until caches rebuild.Can I clear cache without root access?
No. Writing to/proc/sys/vm/drop_caches requires root privileges for system stability and security reasons.
What's the difference between cache and buffer?
Cache stores recently accessed file data; buffer holds temporary data for I/O operations. Both are managed by the kernel and cleared byecho 3.
Is it safe to clear swap on a production server?
Generally not recommended. It can cause temporary unresponsiveness. Only do this during maintenance windows with adequate monitoring.Why use echo 3 instead of 1 or 2?
echo 3 clears all three types of cache (pagecache, dentries, inodes). Use 1 or 2 only if you need to target specific caches.