Linux uses spare RAM to cache recently accessed files, directory paths and file metadata. This speeds up normal operation, but when you are troubleshooting performance, running memory benchmarks or diagnosing an application that is not releasing memory, you may need to clear that cache manually to get an accurate picture of what is happening.
This guide covers how to check current memory usage, flush the kernel cache safely and confirm the result. All commands require root or sudo access.
sudo access on your Linux server.Rather than leaving RAM idle, the Linux kernel fills spare memory with cached data. There are three main types you can clear:
Under normal conditions the kernel reclaims this memory automatically when a running process needs it. Manual clearing is only necessary when you need a clean baseline for testing or when diagnosing a memory-related problem.
Run free before and after clearing the cache so you can compare the results. The -h flag outputs values in human-readable units (MB/GB).
free -h
Note the values in the buff/cache column. This is the memory you will be releasing.
Before dropping the cache, write any pending disk writes to storage using sync. This prevents data loss by flushing buffered writes before the cache is cleared.
sync
/proc/sys/vm/drop_caches to tell the kernel what to release. Three values are available:To clear only the page cache:
echo 1 > /proc/sys/vm/drop_caches
To clear only dentries and inodes:
echo 2 > /proc/sys/vm/drop_caches
To clear the page cache, dentries and inodes together (the most thorough option):
echo 3 > /proc/sys/vm/drop_caches
sync and the cache drop into a single line is the standard approach used in server administration. The && operator runs the second command only after the first succeeds:sync && echo 3 > /proc/sys/vm/drop_caches
The command completes silently. No output means it worked correctly.
Run free again and compare the buff/cache value to your earlier reading. You should see a noticeable reduction.
free -h
The freed memory now appears in the available column, ready for use by running processes.
On non-production servers used for testing or benchmarking, you can schedule cache clearing automatically using a cron job. This removes the need to run the command manually before each test run.
Open the root crontab for editing:
crontab -e
Add the following line to clear the cache every day at midnight:
0 0 * * * sync && echo 3 > /proc/sys/vm/drop_caches
Save and exit the editor. The cron daemon picks up the new job automatically.
Do not schedule this on production servers. Dropping the cache on a live server forces the kernel to reload data from disk, which can cause a temporary spike in I/O and slow response times for active users.
Writing to /proc/sys/vm/drop_caches requires root privileges. If you see a permission denied error, prefix the command with sudo:
sudo sync && sudo sh -c "echo 3 > /proc/sys/vm/drop_caches"
The shell redirection (>) must run under sudo as well, which is why sh -c is used here. Running sudo echo 3 > /proc/sys/vm/drop_caches alone will not work because the redirection is handled by your shell before sudo takes effect.
If the buff/cache column barely changes after running the command, the kernel may have already reclaimed most of that memory for active processes. This is normal behaviour. Linux only holds cached data in memory when it is not needed elsewhere, so the cache may already be smaller than expected before you run the command.
A temporary slowdown after clearing the cache is expected. The kernel must reload file data, directory entries and metadata from disk the next time they are accessed. Performance returns to normal as the cache rebuilds over the following minutes. If slowness persists, the issue is likely unrelated to the cache.
You have checked your server’s memory usage with free -h, flushed pending writes with sync and cleared the Linux kernel cache by writing to /proc/sys/vm/drop_caches. You can now see accurate free memory figures for testing or troubleshooting.
For related server administration tasks, see our guides on checking directory sizes in Linux, checking open ports on Linux and securing your VPS. If you are monitoring memory as part of a broader performance investigation, our guide on switching users in Linux may also be useful when running commands across multiple accounts.
If you need full control over your server environment, take a look at our VPS hosting plans.
Get scalable resources with our VPS hosting with root access and optional software.
Get VPS HostingPerfect for websites and small businesses unlimited bandwidth with cPanel hosting.
Get cPanel Hosting