Skip to content

Instantly share code, notes, and snippets.

@Chaturaphut
Forked from pklaus/clearRAM.sh
Created September 18, 2016 17:12
Show Gist options
  • Save Chaturaphut/422ef580240b9febdeb3d17d01c1db26 to your computer and use it in GitHub Desktop.
Save Chaturaphut/422ef580240b9febdeb3d17d01c1db26 to your computer and use it in GitHub Desktop.

Revisions

  1. @pklaus pklaus revised this gist May 31, 2013. 1 changed file with 19 additions and 51 deletions.
    70 changes: 19 additions & 51 deletions clearRAM.sh
    Original file line number Diff line number Diff line change
    @@ -1,66 +1,34 @@
    #!/bin/bash
    ## Bash Script to clear cached memory on (Ubuntu) Linux
    ## By Philipp Klaus, rewritten by Stuck
    ## 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/>
    ## Minimum Requirements:
    ## - sudo (or being root)
    ## - stdin so running in terminal(-emulator)

    # Usage: output <"Message"> <$X> -- Output depends on running under X or in Command-Prompt
    function output {
    if [ $2 ]; then
    if [ "`which zenity`" ]; then zenity --info --text "$1"
    else
    if [ "`which xmessage`" ]; then xmessage "$1"
    else printf "$1"
    fi
    fi
    else
    printf "$1"
    fi
    }

    sudo=`which sudo`
    # Check Display - are we running X? If so use gksudo
    if [ $DISPLAY ]; then X=true;
    if [ "`which gksudo`" ]; then sudo=`which gksudo`; fi
    fi

    # Check user, script might already be run with sudo or as root
    if [ "`whoami`" == "root" ]; then root=true; fi

    # Is sudo installed? If not, abort unless running as root
    if [ ! "`which sudo`" ] && [ !$root ]; then output "Not Superuser, and sudo not found!" $X; exit 1; fi

    # If not running as root, gksudo not found but not running in terminal (like direct-command-line from Window-Manager), we have a problem:
    # we do not have stdin for sudo-password.
    # Try opening a terminal instead and notify user.
    if [ !$root ] && [ $X ] && [ ! "`which gksudo`" ] && [ ! -t 0 ]
    if [ "$(whoami)" != "root" ]
    then
    output "Ouch! Not running in Terminal, gksudo not found and not running as superuser. Cannot ask for password! Trying to run in new terminal..." $X
    x-terminal-emulator -e $0&
    exit 1
    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`
    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
    output "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." $X
    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."

    # Clear Filesystem Buffer using "sync"
    if [ $root ]; then sync; else $sudo sync; fi
    # Test sync
    if [ "$?" != "0" ]
    then
    echo "Something went wrong, It's impossible to sync the filesystem."
    exit 1
    fi

    # Did sync actually work?
    if [ "$?" != "0" ]; then output "Something went wrong. No or wrong password given, aborting." $X; exit 1; fi
    # Clear Filesystem Buffer using "sync" and Clear Caches
    sync && echo 3 > /proc/sys/vm/drop_caches

    # Clear Caches
    if [ $root ]; then sync; echo 4 > /proc/sys/vm/drop_caches; else $sudo sync; echo 4 | $sudo tee /proc/sys/vm/drop_caches > /dev/null; fi
    freemem_after=`cat /proc/meminfo | grep MemFree | tr -s ' ' | cut -d ' ' -f2` && freemem_after=`echo "$freemem_after/1024.0" | bc`
    freemem_after=$(cat /proc/meminfo | grep MemFree | tr -s ' ' | cut -d ' ' -f2) && freemem_after=$(echo "$freemem_after/1024.0" | bc)

    # Output Summary
    output "This freed `echo "$freemem_after - $freemem_before" | bc` MiB, so now you have $freemem_after MiB of free RAM." $X
    echo -e "This freed $(echo "$freemem_after - $freemem_before" | bc) MiB, so now you have $freemem_after MiB of free RAM."

    exit 0
    exit 0
  2. @pklaus pklaus revised this gist May 12, 2011. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions clearRAM.sh
    Original file line number Diff line number Diff line change
    @@ -57,8 +57,10 @@ if [ $root ]; then sync; else $sudo sync; fi
    if [ "$?" != "0" ]; then output "Something went wrong. No or wrong password given, aborting." $X; exit 1; fi

    # Clear Caches
    echo "sync; echo 4 > /proc/sys/vm/drop_caches" | sudo su
    if [ $root ]; then sync; echo 4 > /proc/sys/vm/drop_caches; else $sudo sync; echo 4 | $sudo tee /proc/sys/vm/drop_caches > /dev/null; fi
    freemem_after=`cat /proc/meminfo | grep MemFree | tr -s ' ' | cut -d ' ' -f2` && freemem_after=`echo "$freemem_after/1024.0" | bc`

    # Output Summary
    output "This freed `echo "$freemem_after - $freemem_before" | bc` MiB, so now you have $freemem_after MiB of free RAM." $X
    output "This freed `echo "$freemem_after - $freemem_before" | bc` MiB, so now you have $freemem_after MiB of free RAM." $X

    exit 0
  3. @pklaus pklaus revised this gist May 12, 2011. 1 changed file with 55 additions and 9 deletions.
    64 changes: 55 additions & 9 deletions clearRAM.sh
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,64 @@
    #!/bin/sh

    #!/bin/bash
    ## Bash Script to clear cached memory on (Ubuntu) Linux
    ## By Philipp Klaus, rewritten by Stuck
    ## see <http://blog.philippklaus.de/2011/02/clear-cached-memory-on-ubuntu/>
    ## Minimum Requirements:
    ## - sudo (or being root)
    ## - stdin so running in terminal(-emulator)

    # Usage: output <"Message"> <$X> -- Output depends on running under X or in Command-Prompt
    function output {
    if [ $2 ]; then
    if [ "`which zenity`" ]; then zenity --info --text "$1"
    else
    if [ "`which xmessage`" ]; then xmessage "$1"
    else printf "$1"
    fi
    fi
    else
    printf "$1"
    fi
    }

    sudo=`which sudo`
    # Check Display - are we running X? If so use gksudo
    if [ $DISPLAY ]; then X=true;
    if [ "`which gksudo`" ]; then sudo=`which gksudo`; fi
    fi

    # Check user, script might already be run with sudo or as root
    if [ "`whoami`" == "root" ]; then root=true; fi

    # Is sudo installed? If not, abort unless running as root
    if [ ! "`which sudo`" ] && [ !$root ]; then output "Not Superuser, and sudo not found!" $X; exit 1; fi

    # If not running as root, gksudo not found but not running in terminal (like direct-command-line from Window-Manager), we have a problem:
    # we do not have stdin for sudo-password.
    # Try opening a terminal instead and notify user.
    if [ !$root ] && [ $X ] && [ ! "`which gksudo`" ] && [ ! -t 0 ]
    then
    output "Ouch! Not running in Terminal, gksudo not found and not running as superuser. Cannot ask for password! Trying to run in new terminal..." $X
    x-terminal-emulator -e $0&
    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`

    zenity --info --text "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."
    # Output Information
    output "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." $X

    gksudo sync
    if [ "$?" != "0" ]; then
    echo "No or wrong password given, aborting."; exit 1
    fi
    sudo sync; echo 4 | sudo tee /proc/sys/vm/drop_caches
    # Clear Filesystem Buffer using "sync"
    if [ $root ]; then sync; else $sudo sync; fi

    # Did sync actually work?
    if [ "$?" != "0" ]; then output "Something went wrong. No or wrong password given, aborting." $X; exit 1; fi

    # Clear Caches
    echo "sync; echo 4 > /proc/sys/vm/drop_caches" | sudo su
    freemem_after=`cat /proc/meminfo | grep MemFree | tr -s ' ' | cut -d ' ' -f2` && freemem_after=`echo "$freemem_after/1024.0" | bc`

    zenity --info --text "This freed `echo "$freemem_after - $freemem_before" | bc` MiB, so now you have $freemem_after MiB of free RAM."
    # Output Summary
    output "This freed `echo "$freemem_after - $freemem_before" | bc` MiB, so now you have $freemem_after MiB of free RAM." $X
  4. @pklaus pklaus revised this gist May 12, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion clearRAM.sh
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ gksudo sync
    if [ "$?" != "0" ]; then
    echo "No or wrong password given, aborting."; exit 1
    fi
    echo "sync; echo 4 > /proc/sys/vm/drop_caches" | sudo su
    sudo sync; echo 4 | sudo tee /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`

    zenity --info --text "This freed `echo "$freemem_after - $freemem_before" | bc` MiB, so now you have $freemem_after MiB of free RAM."
  5. @pklaus pklaus revised this gist Feb 21, 2011. 1 changed file with 8 additions and 10 deletions.
    18 changes: 8 additions & 10 deletions clearRAM.sh
    Original file line number Diff line number Diff line change
    @@ -3,18 +3,16 @@
    ## Bash Script to clear cached memory on (Ubuntu) Linux
    ## see <http://blog.philippklaus.de/2011/02/clear-cached-memory-on-ubuntu/>

    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`
    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`

    zenity --info --text "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."

    gksudo sync
    #echo "sync; echo 3 > /proc/sys/vm/drop_caches" | sudo su
    echo "echo 3 > /proc/sys/vm/drop_caches" | sudo su
    freemem_after=`cat /proc/meminfo | grep MemFree | tr -s ' ' | cut -d ' ' -f2`
    freemem_after=`echo "$freemem_after/1024.0" | bc`

    zenity --info --text "This freed `echo "$freemem_after - $freemem_before" | bc` MiB, so now you have $freemem_after MiB of free RAM."
    if [ "$?" != "0" ]; then
    echo "No or wrong password given, aborting."; exit 1
    fi
    echo "sync; echo 4 > /proc/sys/vm/drop_caches" | sudo su
    freemem_after=`cat /proc/meminfo | grep MemFree | tr -s ' ' | cut -d ' ' -f2` && freemem_after=`echo "$freemem_after/1024.0" | bc`

    zenity --info --text "This freed `echo "$freemem_after - $freemem_before" | bc` MiB, so now you have $freemem_after MiB of free RAM."
  6. @pklaus pklaus created this gist Feb 21, 2011.
    20 changes: 20 additions & 0 deletions clearRAM.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    #!/bin/sh

    ## Bash Script to clear cached memory on (Ubuntu) Linux
    ## see <http://blog.philippklaus.de/2011/02/clear-cached-memory-on-ubuntu/>

    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`

    zenity --info --text "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."

    gksudo sync
    #echo "sync; echo 3 > /proc/sys/vm/drop_caches" | sudo su
    echo "echo 3 > /proc/sys/vm/drop_caches" | sudo su
    freemem_after=`cat /proc/meminfo | grep MemFree | tr -s ' ' | cut -d ' ' -f2`
    freemem_after=`echo "$freemem_after/1024.0" | bc`

    zenity --info --text "This freed `echo "$freemem_after - $freemem_before" | bc` MiB, so now you have $freemem_after MiB of free RAM."