User Agents

Web browsers and everyday web tech, explained

Clear RAM Cache and Swap on Linux

· 4 min read · Cookies and Cache

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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, or vmstat

Step-by-Step

  1. 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.
  2. 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.
  3. Sync filesystems to ensure all pending writes are committed to disk. Run:
    sync
    This prevents data loss when clearing caches and ensures consistency.
  4. Clear pagecache, dentries, and inodes by writing to the drop_caches sysctl 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)
    Note: This does not affect running applications' memory usage.
  5. 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.
  6. 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.
  7. Turn swap back on to re-enable it:
    sudo swapon -a
    This reloads swap space, now empty. You can verify with free -h—swap usage should be near zero.
  8. Optional: Clear only specific caches. Instead of echo 3, use:
    • echo 1 to clear only pagecache
    • echo 2 to clear dentries and inodes only
    Use these if you want more granular control.

Common Mistakes

  • Running drop_caches without syncing: Skipping sync risks 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 -a can 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 sudo results in "Permission denied" errors.
  • Confusing cache with application memory: drop_caches does not kill processes or free memory they’re actively using—only kernel-managed caches.

Tips

  • Monitor memory before and after: Use free -h or htop to 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_caches temporarily: 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 using sudo. The correct full command is echo 3 | sudo tee /proc/sys/vm/drop_caches. Using just sudo echo 3 > /proc/sys/vm/drop_caches fails 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 with top or htop.
  • 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 by echo 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.