Linux Cache Memory: Clear RAM Cache and Swap Safely
In this article
Yes, you can safely clear Linux cache memory and swap, but in most cases you should not need to. Linux uses free RAM for caching to speed up disk access, and the kernel automatically releases cache when applications need memory. Forcing a cache drop or swapoff can be done safely with specific commands, but it usually provides little benefit and may temporarily slow your system.
What Is Linux Cache Memory?
Linux uses a portion of available RAM as page cache, which stores recently read files and disk blocks. This is why free -h often shows very little “free” memory even when no applications are running. The kernel also maintains other caches like dentries (directory entries) and inodes. These caches are not wasted memory—they are reclaimed instantly when a program needs the RAM. The buff/cache column in free shows this usage.
Because the cache is automatically managed, a high cache value is normal and desirable. It improves performance by avoiding repeated disk reads. The kernel uses a complex algorithm to balance cache and free memory, and it rarely needs manual intervention.
Why Would You Want to Clear Cache?
Common reasons users consider clearing cache include:
- Misreading
free -hand thinking the system is out of memory. - After benchmarking, to get a clean state for performance tests.
- When a buggy application or driver seems to hold onto cache incorrectly.
- Before starting a memory-intensive task, to ensure maximum free RAM.
In most everyday scenarios, you do not need to clear cache. The kernel will free memory as needed. Forcing a drop can cause a temporary slowdown because the system must re-read files from disk, which is slower than RAM.
How to View Cache and Swap Usage
Use free -h to see memory usage in human-readable format. The output shows total, used, free, shared, buff/cache, and available memory. The available column is the best indicator of how much memory can be allocated without swapping. Use swapon --show or cat /proc/swaps to see active swap devices and their usage.
To get more detailed cache breakdown, read /proc/meminfo or use vmstat. These tools show how much memory is used for page cache, slab, and other kernel structures.
How to Clear Cache and Swap Safely
Clearing cache is done by writing to /proc/sys/vm/drop_caches. This requires root privileges. The command is:
- Sync filesystems to flush pending writes:
sync - Write a value to drop caches:
echo 3 > /proc/sys/vm/drop_caches
The value can be:
- 1 – free page cache
- 2 – free dentries and inodes
- 3 – free both page cache and dentries/inodes
After running this, the cache is cleared, but it will start to repopulate as files are accessed. This is safe because the kernel treats these as reclaimable pages. However, it is not recommended for routine use.
To clear swap, you can turn it off and on again. This forces the kernel to move swapped-out pages back into RAM. Use:
- Ensure enough free memory is available:
free -h - Turn off swap:
swapoff -a - Turn it back on:
swapon -a
This is safe as long as you have enough free RAM to hold all swapped pages. If you do not, the kernel may fail to swapoff or you may experience OOM (out-of-memory) kills. Always check available memory before doing this.
Common Misconceptions and Pitfalls
A frequent misconception is that high cache usage means the system is low on memory. In reality, the kernel will shrink the cache when applications need memory. Another misconception is that clearing cache improves performance; it often does the opposite because the cache is there to speed up disk I/O.
One real risk with swapoff -a is that if you have more swapped data than free RAM, the operation may fail or cause the OOM killer to terminate processes. Also, if you have a swap file that is not properly configured, turning it off and on may require re-enabling it with swapon /path/to/swapfile.
Another pitfall: writing to drop_caches is not persistent. The kernel will repopulate the cache quickly, so the effect is temporary. Additionally, clearing caches does not free memory that is in use by applications; it only frees reclaimable kernel structures.
When Should You Actually Clear Cache or Swap?
There are a few legitimate scenarios:
- Benchmarking: To measure raw disk performance, you may want to drop caches before each run.
- After a large file operation: If you copied or read a huge file and the cache is full, but you need that RAM for a specific task, you can drop caches.
- Debugging: If you suspect a kernel bug causing excessive cache, you might try dropping it to see if the issue persists.
- Swap troubleshooting: If swap is heavily used but you have plenty of free RAM, you might want to swapoff to force pages back into RAM.
In normal desktop or server use, you can ignore cache and swap usage. The kernel is designed to manage these resources efficiently.
Alternatives to Manual Cache Clearing
Instead of manually clearing cache, you can adjust the kernel’s swappiness and cache pressure settings:
- vm.swappiness (default 60): Controls how aggressively the kernel swaps. Lowering it (e.g., to 10) makes the kernel less likely to swap out processes.
- vm.vfs_cache_pressure (default 100): Controls how much the kernel favors reclaiming inode/dentry caches. Increasing it makes these caches more likely to be freed.
These settings can be changed temporarily with sysctl or permanently in /etc/sysctl.conf. They are safer than manually dropping caches because they let the kernel decide when to reclaim memory.
Frequently Asked Questions
Does clearing cache improve performance?
Usually no. The cache exists to speed up disk access. Clearing it forces the system to re-read files from disk, which is slower. It may improve performance only if you have a specific need for a clean cache, such as benchmarking.
Is it safe to echo 3 to drop_caches?
Yes, it is safe in the sense that it only frees reclaimable memory and does not affect application data. However, it is not recommended for regular use because it provides little benefit and can cause temporary slowdowns.
Why does my free memory show so little when I have no programs open?
Because Linux uses unused RAM for caching. This is normal and beneficial. The available column in free -h shows how much memory is actually available for new applications.
Will clearing swap cause my system to crash?
Only if you do not have enough free RAM to hold all swapped pages. Always check free memory before running swapoff -a. If you have enough, it is safe and will not crash the system.
How often should I clear cache?
You should rarely, if ever, need to clear cache manually. The kernel manages it automatically. Only clear it for specific tasks like benchmarking or debugging.
What is the difference between buff/cache and swap?
Buff/cache is RAM used for disk caching, which is fast and reclaimable. Swap is disk space used to hold memory pages that are not frequently accessed, effectively extending RAM but much slower. Cache is good; swap is a fallback.