
How to Safely Clear Memory Cache in Linux to Boost System Performance
If you’re a Linux system administrator or power user, you’ve likely used commands like free
or top
and noticed that your system’s memory usage seems consistently high. While this can sometimes be a cause for concern, it’s often just the Linux kernel doing its job efficiently by using free RAM for disk caching.
However, there are specific situations—like benchmarking, testing an application’s memory footprint, or resolving certain performance issues—where you might need to manually clear this cache without rebooting the entire system. This guide will walk you through how to safely and effectively clear the RAM cache, buffers, and even swap space in Linux.
Understanding Linux Memory Usage
Before we start clearing anything, it’s crucial to understand what you’re looking at. The best command for a quick, human-readable overview of your memory is free -h
.
$ free -h
total used free shared buff/cache available
Mem: 7.7Gi 3.9Gi 1.2Gi 143Mi 2.6Gi 3.4Gi
Swap: 2.0Gi 512Mi 1.5Gi
Here’s a quick breakdown of the important columns:
- total: The total amount of physical RAM installed.
- used: Memory currently in use by running applications.
- free: Memory that is completely unused.
- buff/cache: This is the key one. It’s memory being used by the kernel for buffers and caching disk operations to speed things up. This is the memory we can clear.
- available: An estimation of how much memory is available for starting new applications without swapping. It includes the
free
memory plus a portion of thebuff/cache
that can be reclaimed easily.
As you can see, high usage in the buff/cache
column is normal. Linux is smart; it knows that unused RAM is wasted RAM. By caching frequently accessed files, it dramatically improves system performance.
How to Clear the RAM Cache in Linux
Clearing the cache involves writing to a specific file in the /proc
filesystem. Because this is a system-level operation, you will need root or sudo
privileges for these commands.
A crucial first step is to sync the disk. Before clearing the cache, it’s best practice to run the sync
command. This flushes all unwritten data from the memory buffer to the disk, preventing any potential data loss.
sudo sync
After the sync
command completes, you can proceed with clearing the cache using the drop_caches
file. You can choose to clear different parts of the cache by writing a number (1, 2, or 3) to this file.
1. To clear the PageCache only:
The PageCache stores the data of files you have recently read. This is the most common and safest cache to clear.
sudo echo 1 > /proc/sys/vm/drop_caches
2. To clear dentries and inodes:
The kernel uses dentries (directory entries) and inodes to cache filesystem information, which helps speed up file lookups.
sudo echo 2 > /proc/sys/vm/drop_caches
3. To clear the PageCache, dentries, and inodes:
This is the most comprehensive option and will clear all reclaimable kernel caches. This is the command most people use when they need to free up the maximum amount of cached memory.
sudo echo 3 > /proc/sys/vm/drop_caches
After running one of these commands, you can execute free -h
again to see the immediate effect. You should notice a significant drop in the buff/cache
column and a corresponding increase in the free
column.
How to Clear Swap Space
If your system has started using swap space (memory on the hard disk), clearing it can also improve performance, especially if the processes that were using it are no longer running. Clearing swap involves temporarily turning it off and then back on.
Warning: This process will move all data from the swap back into your physical RAM. Ensure you have enough free RAM available before proceeding, otherwise your system could become unstable or crash.
Turn off all swap areas:
sudo swapoff -a
Turn swap back on:
bash
sudo swapon -a
After these commands complete, run free -h
again. You should see that the used swap space is now zero or significantly reduced.
A Word of Caution: Should You Clear the Cache Regularly?
While knowing how to clear the cache is a valuable skill, it’s important to understand when and why to use it.
You should NOT clear the cache as a routine maintenance task. The Linux kernel’s memory management is highly optimized. The cache exists to make your system faster. By clearing it, you are forcing the system to read data from the much slower disk the next time it’s needed, which will temporarily decrease performance.
So, when is it appropriate to clear the cache?
- Application Benchmarking: When you need to measure the performance of an application on a “clean” system to get accurate disk I/O metrics.
- Troubleshooting: If you suspect a memory leak in an application or a kernel module is causing the cache to grow uncontrollably and not release memory when needed.
- Resource-Intensive Tasks: If you are about to run a task that you know will require a massive amount of RAM (like compiling a large program or running a virtual machine), clearing the cache beforehand can ensure it has the maximum available memory from the start.
In summary, clearing the RAM cache and swap in Linux is a powerful tool for system administrators. It provides direct control over memory management for specific, targeted situations. By using these commands wisely, you can effectively troubleshoot issues and optimize your system for demanding tasks without needing a full reboot.
Source: https://www.tecmint.com/clear-ram-memory-cache-buffer-and-swap-space-on-linux/