Clearing Linux memory cache and swap

Every Linux system has three options when clearing cache without interrupting any processes or application services.

Linux over the years has improved and implemented memory management efficiently and in most of cases, you don't have to worry about system memory buffering.

However, in some cases, you may want to "manually flush" out the cache in the memory. For example, during benchmarking, you want to clear buffered cache before you do benchmarking. Linux provides a way to flush or clear ram cache.

Clearing Linux Cache

The three options available to clear Linux cache are as follows:

  1. page cache only
  2. inodes and dentries
  3. page cache, inodes and dentries

Clearing page cache only

The page cache may contain any memory mappings to blocks on disk. That could conceivably be buffered I/O, memory mapped files, paged areas of executables--anything that the OS could hold in memory from a file.

If you need to clear disk cache (page cache) only then this is the safest method in an enterprise/production environment.

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

The above command simply flushes the file system buffer using the sync command and then clears the page cache.

Clearing inodes and dentries

An inode in this context is a data structure that represents a file. A dentries is a data structure that represents a directory. These structures could be used to build a memory cache that represents the file structure on a disk. To get a directly listing, the OS could go to the dentries--if the directory is there--list its contents (a series of inodes). If not there, go to the disk and read it into memory so that it can be used again.

To clear Linux inodes and dentries, we simply run:

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

Clearing page cache, inodes and dentries

To clear all cache entries, we can simply run:

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

CAUTION: It is not recommended to use third option above in a production environment until you understand what you are doing, as it will clear page cache, inodes and dentries.

Clearing swap space

Clearing swap space on a linux system is as easy as ABC... Simply using the swapoff/swapon commands. For example:

# swapoff -a && swapon -a

In Conclusion:

To simplify things a little bit: "free" memory can also be seen as "unused", or even more dramatic - a waste of resources: you paid for it, but don't make use of it. That's a very un-economic situation, and the linux kernel tries to make some "more useful" use of your "free" memory.

Part of its strategy involves using it to save various kinds of disk I/O by using various dynamically sized memory caches. A quick access to cache memory saves "slow" disk access, so that's often a useful idea.

As soon as a "more important" process wants to allocate memory, the Linux kernel voluntarily frees those caches and makes the memory available to the requesting process. So there's usually no need to "manually free" those caches.

The Linux kernel may even decide to swap out memory of an otherwise idle process to disk (swap space), freeing RAM to be used for "more important" tasks, probably also including to be used as some cache.

So as long as your system is not actively swapping in/out, there's little reason to manually flush caches.

A common case to "manually flush" those caches is purely for benchmark comparison: your first benchmark run may run with "empty" caches and so give poor results, while a second run will show much "better" results (due to the pre-warmed caches). By flushing your caches before any benchmark run, you're removing the "warmed" caches and so your benchmark runs are more "fair" to be compared with each other.