How To Clear Cache Memory In Linux

  



You can write to /proc/sys/vm/drop_caches file to instruct kernel to drop clean caches, as well as reclaimable slab objects like dentries and inodes. Once dropped, their memory becomes free.

This is not recommended to clear memory cache on Linux systems, but it is safe. But clearing cache may cause performance issue with system. Since it discards cached objects from memory, it may cost a significant amount of I/O and CPU to recreate the dropped objects.

This tutorial will help you to clear memory cache on Linux/Unix system via command line.

How To Clear Cache Memory In Linux Server

How to Clear Cache in Linux System

How To Clear Cache Memory In Linux
  • Oct 12, 2020 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.dropcaches=1 To clear dentries and inodes, use this command: $ sudo sysctl vm.dropcaches=2.
  • How to Clear Memory Cache on Linux. By default, every Linux OS has an efficient memory management system used to clear the buffer cache periodically. You can manually free up the memory cache with the following simple command: sk@sk:$ sudo sh -c 'sync; echo 3 /proc/sys/vm/dropcaches'.

How to set a cron job to clear cache memory in Linux? The cache memory may be required to cleaned up at regular intervals and free up the disk space. The same can be achieved by creating a cronjob to automatically clear the cache memory using crontab. The page cache is automatically enabled on Linux systems and will transparently make IO faster by storing recently used data in the cache. If you want to manually clear the cache that can be done easily by sending an echo command to the /proc filesystem indicating to the kernel to drop the cache and free the memory used for the cache. Jun 28, 2016 Like any other operating system, GNU/Linux has implemented a memory management efficiently and even more than that. But if any process is eating away your memory and you want to clear it, Linux provides a way to flush or clear ram cache.

There are three options available to clear cache in Linux system memory. Use one of below as per your requirements.

  • Clear PageCache, dentries and inodes in cache memory
  • Clear dentries and inodes only in cache memory
  • Clear pagecache only in cache memory

Here the sync command is used to increase the number of objects freed by the drop cache. Using this a user can claim more memory by clearing more dirty objects on the system.

How to Schedule Clear Memory Cache

If you have to clear buffer cache regularly, use the cronjob do it. Schedule the following in system crontab to automatically flush cache memory on a regular interval.

Open a terminal and execute ‘crontab -e’ command to edit crontab:

How to clear cache memory in linux

Append below entry to the file:

The above cron will execute on every hour and flushes the memory cache on your system.

How to find Cache Memory in Linux

Use free command to find out cache memory uses by Linux system. Output of free command is like below

Linux Command To Clear Memory

Output:

How To Clear Cache Memory In Linux Operating System

Here the last column is showing cached memory (12953 MB) on Linux system. The -m option is used to show output MB’s.