Skip to content

Instantly share code, notes, and snippets.

@craig-m
Created November 22, 2015 10:50
Show Gist options
  • Save craig-m/e1ccd687a81b3f9a8c30 to your computer and use it in GitHub Desktop.
Save craig-m/e1ccd687a81b3f9a8c30 to your computer and use it in GitHub Desktop.
Drop cached memory
#!/bin/bash
#
# Writing to this will cause the kernel to
# free pagecache, dentries and inodes.
# This causes that memory to become free.
#
# non-destructive & safe for production:
# ref: https://www.kernel.org/doc/Documentation/sysctl/vm.txt
freebefore=`free -k | awk '/^Mem:/{print $4}'`
# need to sync first
sync
echo 3 > /proc/sys/vm/drop_caches
freeafter=`free -k | awk '/^Mem:/{print $4}'`
freekb=`expr $freeafter - $freebefore`
let freemb=($freekb/1024)
echo "freed $freekb kilobytes ($freemb MB)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment