How to Clear Memory Cache in Linux

Someone looks at free -h, sees almost no free memory, and assumes the server is in trouble. It usually isn’t. Linux fills spare RAM with cached file data on purpose, because memory sitting empty is memory doing nothing, and it hands that cache straight back the moment an application asks for it.

So before going any further: if you are here because used looks high, you probably don’t need any of this. Check the available column in free -h instead. If that number is healthy, the system has room and the cache is doing exactly what it was designed to do.

There are still legitimate reasons to drop it. Benchmarking is the main one, where you want a cold cache so a disk read is genuinely a disk read. Reproducing a slow first-load. Confirming what a number actually represents when you are chasing a memory question. Linux gives you three levels of control, and none of them interrupt running processes or services.


1. Clear PageCache Only

sync; echo 1 > /proc/sys/vm/drop_caches

  • What it does: Frees page cache (cached files).
  • Use case: When you want to clear cached file data.

This is the one that reclaims the most, because on a busy server the page cache is usually the bulk of it. It is also the one that costs you the most afterwards, since every file the system was holding has to come off disk again the next time it is wanted.


2. Clear Dentries and Inodes

sync; echo 2 > /proc/sys/vm/drop_caches

  • What it does: Frees dentries (directory entries) and inodes (file metadata).
  • Use case: When you want to clear metadata, but not the actual cached files.

Worth knowing what these actually are, because the names give nothing away. Dentries are the kernel’s cached answers to “which inode does this path point at”, and inodes hold the metadata itself: size, owner, timestamps, where the blocks live. Together they are why the second ls of a large directory returns instantly and the first one takes a moment.

Drop them and that lookup work all has to happen again. On a filesystem holding a lot of small files, that is more noticeable than you would expect.


3. Clear PageCache, Dentries, and Inodes

sync; echo 3 > /proc/sys/vm/drop_caches

  • What it does: Frees page cache, dentries, and inodes.
  • Use case: When you want to clear everything cache-related.

Everything at once, and the one people reach for by default. If you are setting up a benchmark this is the right choice, because a half-warm cache makes the first run meaningless.


Important Notes:

  • Always run sync before clearing cache to make sure all cached data is written to disk, preventing potential data loss.
  • You need root privileges to execute these commands. A plain sudo in front of the echo is not enough, because the redirect happens in your own shell rather than under sudo, so wrap the whole thing instead.
  • This is generally safe, but clearing the cache might slow down your system temporarily as it will need to reload files into memory.

That second point catches nearly everyone once. You run sudo echo 3 into the file, you get permission denied, and it looks like sudo isn’t working. It is working fine. The shell opens the output file before sudo ever runs, and your shell is not root. Wrapping the pair of commands in a shell that is running as root is what fixes it.

One more thing worth saying plainly. Dropping caches is not a fix for anything. It frees memory that was already available, so the available figure barely moves, and the machine is slower for a while afterwards. If a server genuinely is under memory pressure you will see it in vmstat, in the si and so columns, where sustained activity means real swapping. That is a problem worth solving. A full page cache is not.

Which is also why this does not belong in cron. Scheduling a cache drop means paying the reload cost over and over for no benefit, and it makes real memory problems harder to spot rather than easier.


Summary Table:

Cache to Clear Command
PageCache only sync; echo 1 > /proc/sys/vm/drop_caches
Dentries and Inodes only sync; echo 2 > /proc/sys/vm/drop_caches
PageCache, Dentries, Inodes sync; echo 3 > /proc/sys/vm/drop_caches

 

Before dropping anything, it is worth being sure the numbers mean what you think they mean: understanding the free command explains which column actually indicates memory pressure.

Avatar photo

Asif Khan

I have spent over 10 years working across IT systems, open source software, DevOps, Linux administration and cloud operations. Three things drive most of what I do: automation, security and resilience. Much of that work involves planning and building the platforms that sit behind services people rely on daily, which means designing for failure just as carefully as for load. Cloud computing held my attention early on, largely for its flexibility. Being able to scale up and then back down again means far less guessing about how much capacity you will need. Across projects I work with the full DevOps toolchain, from provisioning, orchestration and configuration management through to release management and microservices architecture.