Freeing Memory buffers and cache in Linux


#!/bin/bash
sync
echo 3 > /proc/sys/vm/drop_caches

Why would you want to do that? Linux uses buffers and cache intelligently – unused memory is wasted memory.
Yes, I do understand all of this. I found this to be useful when hibernating with TuxOnIce. Calling this script before hibernating results in much faster hibernates and resumes.
If using ArchLinux and hibernate-script a good place to put this would be /etc/hibernate/common.conf – add the following at this point in the file:

### misclaunch
# OnSuspend 20 echo "Good night!"
# OnResume 20 echo "Good morning!"
OnSuspend 95 /path/to/above/script

3 Responses to “Freeing Memory buffers and cache in Linux”

  1. /usr/src/linux/Documentation/filesystems/proc.txt recommends to `sync’ before dropping caches: «as this is a non-destructive operation and dirty objects are not freeable, the user should run `sync’ first.»

  2. For suspend2ram users:
    -/etc/hibernate/common.conf
    +/etc/hibernate/hibernate.conf

    And there is better place to do that:
    -OnSuspend 5 /path/to/above/script
    +OnSuspend 95 /path/to/above/script

  3. Thanks for comment – have adjusted posts accordingly

Leave a Reply