Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Script to Clear Cached RAM on Linux
#!/bin/bash
## Bash Script to clear cached memory on (Ubuntu/Debian) Linux
## By Philipp Klaus
## see <http://blog.philippklaus.de/2011/02/clear-cached-memory-on-ubuntu/>
if [ "$(whoami)" != "root" ]
then
echo "You have to run this script as Superuser!"
exit 1
fi
# Get Memory Information
freemem_before=$(cat /proc/meminfo | grep MemFree | tr -s ' ' | cut -d ' ' -f2) && freemem_before=$(echo "$freemem_before/1024.0" | bc)
cachedmem_before=$(cat /proc/meminfo | grep "^Cached" | tr -s ' ' | cut -d ' ' -f2) && cachedmem_before=$(echo "$cachedmem_before/1024.0" | bc)
# Output Information
echo -e "This script will clear cached memory and free up your ram.\n\nAt the moment you have $cachedmem_before MiB cached and $freemem_before MiB free memory."
# Test sync
if [ "$?" != "0" ]
then
echo "Something went wrong, It's impossible to sync the filesystem."
exit 1
fi
# Clear Filesystem Buffer using "sync" and Clear Caches
sync && echo 3 > /proc/sys/vm/drop_caches
freemem_after=$(cat /proc/meminfo | grep MemFree | tr -s ' ' | cut -d ' ' -f2) && freemem_after=$(echo "$freemem_after/1024.0" | bc)
# Output Summary
echo -e "This freed $(echo "$freemem_after - $freemem_before" | bc) MiB, so now you have $freemem_after MiB of free RAM."
exit 0
@blueapple299

This comment has been minimized.

Copy link

blueapple299 commented Aug 22, 2017

Newbie to UNIX.

Thanks a Lot !!!

@GajjarAkash

This comment has been minimized.

Copy link

GajjarAkash commented Sep 15, 2017

wow,

@beingak

This comment has been minimized.

Copy link

beingak commented Nov 27, 2017

We need to have bc installed to run the script
sudo apt install bc

@govind0229

This comment has been minimized.

Copy link

govind0229 commented Dec 22, 2017

Thanks a lot...!!

@juri74

This comment has been minimized.

Copy link

juri74 commented Nov 1, 2018

hello!
i copy-pasted your script to a file (named flushram.sh) then i chmodded it to 777, but when i run it i get this output and cache were not cleared :(
any clues?

root@raspberrypi:/home# sudo sh flushram.sh
flushram.sh: 13: flushram.sh: bc: not found
flushram.sh: 14: flushram.sh: bc: not found
This script will clear cached memory and free up your ram.

At the moment you have  MiB cached and  MiB free memory.
flushram.sh: 29: flushram.sh: bc: not found
flushram.sh: 32: flushram.sh: bc: not found
This freed  MiB, so now you have  MiB of free RAM.
root@raspberrypi:/home#

Many thanks for any help
Juri

@krishanranditha

This comment has been minimized.

Copy link

krishanranditha commented Oct 17, 2019

hello!
i copy-pasted your script to a file (named flushram.sh) then i chmodded it to 777, but when i run it i get this output and cache were not cleared :(
any clues?

root@raspberrypi:/home# sudo sh flushram.sh
flushram.sh: 13: flushram.sh: bc: not found
flushram.sh: 14: flushram.sh: bc: not found
This script will clear cached memory and free up your ram.

At the moment you have  MiB cached and  MiB free memory.
flushram.sh: 29: flushram.sh: bc: not found
flushram.sh: 32: flushram.sh: bc: not found
This freed  MiB, so now you have  MiB of free RAM.
root@raspberrypi:/home#

Many thanks for any help
Juri

You need to install "bc" to run this script.
The “bc” is the command-line calculator for Linux. You can simply do calculations through your command-line.
If your OS is Redhat/Centos use >>> "yum install bc"
or Ubuntu >> "apt-get install bc"

@xano75

This comment has been minimized.

Copy link

xano75 commented Dec 16, 2019

image

@xano75

This comment has been minimized.

Copy link

xano75 commented Dec 16, 2019

Permission denied

@xgportal

This comment has been minimized.

Copy link

xgportal commented Feb 16, 2020

Works like a charm, thanks.

Just make sure to install bc !!

sudo apt-get install bc

then run script as follows:

sudo ./flushram.sh

My result:

At the moment you have 223 MiB cached and 2425 MiB free memory.
This freed 63 MiB, so now you have 2488 MiB of free RAM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.