How to delete memory buffers and cache in Ubuntu 20.04 LTS?[CLI]

Hello folks, In this article, we will be discussing how to delete memory buffers and cache in Ubuntu 20.04 LTS. Linux has very efficient memory management but if some process is taking too much memory, you can clear the memory by deleting buffers and cache. Note that deleting the memory buffers and cache manually will reduce the performance so only do it if necessary.

Checking the memory usage

We can check the memory usage using the free command which shows total, free, used, shared, buffer/cache, and available memory. Open a terminal by pressing Ctrl+Alt+T. Execute the following command to check the memory usage,

free

or

free -h
delete memory-buffer-cache-1

By using -h parameter, It shows human-readable data as you can see above. Here, The size of the memory buffer and cache is 4.0Gi.

Deleting memory buffers and cache via terminal

Open a terminal by pressing Ctrl+Alt+T. Enter the following commands to empty the memory buffers.

Enter the following command to login as root and enter the password if prompted.

sudo su
delete memory-buffer-cache-2

To empty the page cache, enter the following command,

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

To empty entries and inodes, enter the following command,

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

To empty page cache, entries, and inodes at once, enter the following command.

# echo 3 > /proc/sys/vm/drop_caches
delete memory-buffer-cache-3

To empty the memory buffer and cache, execute the following command,

# free && sync && echo 3 > /proc/sys/vm/drop_caches && free
delete memory-buffer-cache-4

To empty the memory buffers and cache using the sudo command, enter the commands as shown,

sudo sh -c 'echo 1 >/proc/sys/vm/drop_caches'
sudo sh -c 'echo 2 >/proc/sys/vm/drop_caches'
sudo sh -c 'echo 3 >/proc/sys/vm/drop_caches'
delete memory-buffer-cache-5

That’s all! The size of the memory buffer and cache has been decreased from 4.0GB to 920 MB as shown.

delete memory-buffer-cache-6

Conclusion

So, we discussed how to clear the memory buffers and cache in Linux. However, It reduces the overall performance so try not to do it frequently. Thank you for reading!