izzakU

FreeRAM.sh

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