When files and system utilities are used by a Linux system, they’re temporarily stored in random access memory (RAM), which makes them much quicker to access. This is a good thing, since frequently accessed information can be quickly recalled, which ultimately makes your system perform faster.
The operating system determines how long to keep files cached in memory, but Linux also gives the user the ability to clear the cache manually. You normally will never need to do this, although there are edge situations where it can come in handy.
In this guide, we’ll show you how to clear the memory cache on Linux by clearing PageCache, dentries, and inodes from the command line.
In this tutorial you will learn:
- What is PageCache, dentry, and inode cache?
- How to clear cache
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
What is PageCache, dentry, and inode cache?
There are three types of caches that we’ll be learning to clear in this guide. You should start by familiarizing yourself with the following terms.
PageCache is cached files. Files that were recently accessed are stored here so they will not need to be queried from the hard disk again, unless that file changes or the cache is cleared to make room for other data. This puts less read and write impact on a hard drive and increases speed since files can be read from RAM much faster.
Dentry and inode cache is directory and file attributes. This information goes hand in hand with PageCache, although it doesn’t contain the actual contents of any files. This cache also cuts down on disk input and ouput operations.
How to clear cache
The majority of the most popular Linux distros use systemd these days, thus a systemctl command can be used to clear the memory cache.
To clear PageCache only, use this command:
$ sudo sysctl vm.drop_caches=1
To clear dentries and inodes, use this command:
$ sudo sysctl vm.drop_caches=2
To clear PageCache, plus dentries and inodes, use this command:
$ sudo sysctl vm.drop_caches=3
You can now use the
free command or
top to check your system’s RAM usage and verify that the cache has been cleared.
If you are running a system that doesn’t use systemd, you can use the following commands to accomplish the same thing as the respective systemctl commands:
# sync; echo 1 > /proc/sys/vm/drop_caches # clear PageCache # sync; echo 2 > /proc/sys/vm/drop_caches # clear dentries and inodes # sync; echo 3 > /proc/sys/vm/drop_caches # clear all 3
In this article, we saw how to clear the memory cache on a Linux system. We learned how to use systemd as well as another procedure to accomplish the job. Clearing the cache is a simple task but one that only needs to be done in rare situations, such as with development or troubleshooting.