Home Elementary OS How to clear computer’s RAM in Ubuntu and Linux Mint

How to clear computer’s RAM in Ubuntu and Linux Mint

by Kiran Kumar
Published: Updated:
clear ram ubuntu

By default settings, Linux has a very efficient way of managing the computer’s RAM to get the best performance out of the available resources. Although this approach is excellent, it might be confusing to users as the memory looks entirely occupied all the time in spite of closing all applications.

Linux works this way. It utilizes available memory to cache the apps from the hard disk drive, in a goal to perform faster.

The same advantage becomes a frustrating ordeal, especially for system administrators who are troubleshooting a PC. The changes applied to the system files in the hard disk may not be read because Linux is loading it from the RAM.

Therefore, during the troubleshooting process when one is in the process of confirming a fix to a problem, it is a good idea to clear the memory immediately to check result. A reboot will work too, but that’s a lot of time wastage and includes killing the current session. Instead, how about wiping the RAM in the same session to save time? Let’s take a look.

Clearing RAM in Ubuntu, Linux Mint, and derivatives

Launch Terminal and enter the following command.

sudo sync; echo 3 > /proc/sys/vm/drop_caches

The command ‘sync’ is flushing the file system buffer. Command ‘echo’ is doing the job of writing to file and additionally, drop_cache is deleting the cache without killing any application/service. You should immediately see RAM getting freed-up.

Tip: Automate Freeing up RAM by Cron Job

Now that you know how to free up memory on your computer, you may want to automate the clearing memory process at a particular frequency. It can quickly be done by applying a cron process. Note that I strongly advise NOT to apply a cron job on server machines! It could corrupt the server data. It is to be used only on desktop and laptop PCs.

STEP 1: To get started, launch terminal and enter the following command to install vim. It is a preferred text editor to create sh files.

sudo apt-get install vim

Pay attention to the terminal and enter the root password and ‘Y’ when requested.

STEP 2: Now let’s create a sh file named clearram.sh where we will be adding the script.

vim clearram.sh

STEP 3: In the vim editor, you need to press ‘esc’ key and then press ‘i’ to enter into INSERT mode. Then add the following script to it. The first line below is the Shebang, and then the command which we used to clear RAM.

#!/bin/bash
echo "echo 3 > /proc/sys/vm/drop_caches"

Save the sh file and exit out of the vim text editor. To do that type :wq and press enter. Vim will save the sh file and exit out to the terminal.

STEP 4: Now enter the following command in the terminal to give read/write permissions.

sudo chmod 755 clearram.sh

STEP 5: Time now to call the crontab command:

sudo crontab -e

STEP 6: Let’s assume that we want to clear the RAM every day at 1 PM.

0 13 * * * /clearram.sh

By default, the new sh file will be created at the top level home directory. You can move it to another place, but do remember the path you give in the above command.

That’s it!

You may also like

17 comments

Gaurab Santra July 19, 2018 - 11:42 AM

It is giving an output mentioedbelow

“bash: /proc/sys/vm/drop_caches: Permission denied”

What to do?

Reply
Niels Engdahl Jensen July 20, 2018 - 4:39 PM

It seems like, you have to bee root.

Reply
Niels Engdahl Jensen July 20, 2018 - 5:11 PM

I am sorry for my stupid comment.
Step 4 is now working for me.

Reply
t76t76t November 10, 2018 - 2:03 PM

bash: /proc/sys/vm/drop_caches: Permission denied

Reply
Nesta May 6, 2020 - 3:05 AM

chmod 777 clearam.sh

Reply
Shreeya June 18, 2019 - 12:32 PM

I used: sudo sh -c ‘echo 3 >/proc/sys/vm/drop_caches’

Reply
Aman August 19, 2020 - 3:13 PM

Have you seen your website ever?It seems like a macchi market of ads..Its one of the worst performing website in the world in today’s time.In last 3-4 years I found your website only which takes 4-5 minutes in opening a single page and here you are giving lecture how we can improve performance of our system?

Reply
Ayaan August 18, 2021 - 12:56 PM

Your internet connection is slow

Reply
WarGas October 1, 2020 - 6:19 AM

sudo sh -c ‘sync; echo 3 > /proc/sys/vm/drop_caches’

OR

sudo echo 3 | sudo tee /proc/sys/vm/drop_caches

Reply
Joey January 29, 2021 - 9:48 AM

” 0 13 * * * /clearram.sh ” What does it mean?please

Reply
Randall February 3, 2021 - 6:08 PM

I go in as admin sudo su and then your password..

Reply
anupam taneja February 9, 2021 - 4:37 PM

This is by far the best Linux tip on the entire internet.
Thanks a lot!

Reply
Andrew Nyago March 19, 2021 - 6:46 PM

I am using Linux 20.04.2 and im getting : “zsh: no such file or directory: /proc/sys/vm/drop_caches’”

Reply
Harry D April 6, 2021 - 7:53 AM

The RAM clear command is not working on Ubuntu 20.04 LTS.
Gives permission denied error even with sudo command and user password.

Reply
linuxguy May 2, 2021 - 12:49 PM

doesn’t work, i get permission denied even using sudo

Reply
anzacpi December 13, 2021 - 3:42 PM

Does not work on my IMac running Ubuntu 21.10

Reply
David Lockett November 25, 2022 - 8:12 PM

It may be a good idea if, in Ubuntu, memory could be cleared from a simple interface-issued command instead of needing to go into the terminal and insert what many would find to be complex and confusing code. And if a popup window displayed a warning such as ‘you are running low on memory, do you want to clear the cache’ or something similar. These types of issues are the reason why Linux is not popular among the masses. Linux developers should read the book “Don’t make me think” by Steve Krugg and they may learn a thing or two about interface design.

Reply

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.