Beginner Extended

How to clear cache and free memory in Linux

By Angus Published 14 May 2026 5 min read

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.

Before you begin

  • You need root or sudo access on your Linux server.
  • These steps apply to any mainstream Linux distribution (Ubuntu, Debian, AlmaLinux, CentOS).
  • Clearing the cache is safe, but the system will run slightly slower immediately afterwards while it rebuilds the cache from disk. Avoid doing this on a busy production server unless necessary.
  • We recommend connecting over SSH before running these commands. See our guide on how to connect and use SSH if you need help.

How Linux memory caching works

Rather than leaving RAM idle, the Linux kernel fills spare memory with cached data. There are three main types you can clear:

  • Page cache – stores the contents of files read from disk. Subsequent reads of the same file come from RAM instead of the slower storage device.
  • Dentries – cached directory entries that speed up path lookups.
  • Inodes – cached file metadata such as permissions, ownership and timestamps.

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.

Check memory usage before clearing the cache

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.

Clear the Linux cache safely

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.

  1. Flush pending writes to disk.
    Run the following command to write all buffered data to storage before touching the cache:
sync
  1. Choose what to clear.
    Write a value to /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
  1. Run commands together.
    Combining 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.

Confirm the cache has been cleared

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.

Automate cache clearing with cron

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.

Troubleshooting

Permission denied when writing to drop_caches

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.

buff/cache value does not drop significantly

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.

Server becomes slow after clearing the cache

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.

Wrapping up

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.

Need more power?

Get scalable resources with our VPS hosting with root access and optional software.

Get VPS Hosting

Starting something new?

Perfect for websites and small businesses unlimited bandwidth with cPanel hosting.

Get cPanel Hosting