Advertisement
TVT618

Linux RAM cleaner bashshell

Jun 1st, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.34 KB | None | 0 0
  1. #!/bin/bash
  2. ## Bash Script to clear cached memory on (Ubuntu/Debian) Linux
  3. ## By Philipp Klaus
  4. ## see <http://blog.philippklaus.de/2011/02/clear-cached-memory-on-ubuntu/>
  5. ##Source: https://gist.github.com/pklaus/837023
  6. if [ "$(whoami)" != "root" ]
  7. then
  8.   echo "You have to run this script as Superuser!"
  9.   exit 1
  10. fi
  11.  
  12. # Get Memory Information
  13. freemem_before=$(cat /proc/meminfo | grep MemFree | tr -s ' ' | cut -d ' ' -f2) && freemem_before=$(echo "$freemem_before/1024.0" | bc)
  14. cachedmem_before=$(cat /proc/meminfo | grep "^Cached" | tr -s ' ' | cut -d ' ' -f2) && cachedmem_before=$(echo "$cachedmem_before/1024.0" | bc)
  15.  
  16. # Output Information
  17. 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."
  18.  
  19. # Test sync
  20. if [ "$?" != "0" ]
  21. then
  22.   echo "Something went wrong, It's impossible to sync the filesystem."
  23.   exit 1
  24. fi
  25.  
  26. # Clear Filesystem Buffer using "sync" and Clear Caches
  27. sync && echo 3 > /proc/sys/vm/drop_caches
  28.  
  29. freemem_after=$(cat /proc/meminfo | grep MemFree | tr -s ' ' | cut -d ' ' -f2) && freemem_after=$(echo "$freemem_after/1024.0" | bc)
  30.  
  31. # Output Summary
  32. echo -e "This freed $(echo "$freemem_after - $freemem_before" | bc) MiB, so now you have $freemem_after MiB of free RAM."
  33.  
  34. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement