How to Clear cache on Linux

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 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

Conclusion
Clearing the cache is a simple task but one that only needs to be done in rare situations, such as with development or troubleshooting.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s