How to free RAM and cache in Linux with one command

There are a few ways by which you can clear RAM in Linux like from system monitor and htop. In this tutorial you will know how to clear the ram and cache using one command.

How to check how RAM info in terminal

To check the current state of RAM in terminal just type ‘free’ use -m switch to display output in megabytes.

free -m
              total        used        free      shared  buff/cache   available
Mem:           3760        2363         144         489        1252         669
Swap:          2047        1079         968

Command to clear the cache

To clear the cache type the command in terminal and it requires root permission.

free -m && sync && echo 3 > /proc/sys/vm/drop_caches && free -m
              total        used        free      shared  buff/cache   available
Mem:           3760        2363         144         489        1252         669
Swap:          2047        1079         968
bash: /proc/sys/vm/drop_caches: Permission denied

To run this command in root type

sudo su
              total        used        free      shared  buff/cache   available
Mem:           3760        2408         173         503        1178         610
Swap:          2047        1105         942
              total        used        free      shared  buff/cache   available
Mem:           3760        2414         458         494         887         616
Swap:          2047        1105   

It will show before and after effect of command.

How to make the command short

This command may look lengthy and hard to remember, to make it short follow the steps below.

Create a file with name free_ram we are using vim as a text editor you can use any of your choice.

vim free_ram

And write the command in this file.

 free -m && sync && echo 3 > /proc/sys/vm/drop_caches && free -m
     

save and exit.

Now give this file executable permissions

sudo chmod +x free_ram

Now whenever you need to clear your cache or buffer or free some RAM just open this file as

./free_ram

Open this file inside root. Use sudo su to enter root.

Related Posts

23 thoughts on “How to free RAM and cache in Linux with one command

  1. New article idea how to delete windows files from ubuntu in a dual boot setup i searched mam but didn’t find good articles only found a yt vid

Leave a Reply

Your email address will not be published.