Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
LaunchAgent to release inactive memory on OS X 10.7
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">
#!/usr/bin/env python
import os
import re
import sys
from subprocess import call, Popen, PIPE
INACTIVE_THRESHOLD = 1024 # Number of MBs
RE_INACTIVE = re.compile('Pages inactive:\s+(\d+)')
RE_FREE = re.compile('Pages free:\s+(\d+)')
RE_SPECULATIVE = re.compile('Pages speculative:\s+(\d+)')
LOCK_FILE = '/var/tmp/releasemem.lock'
def acquire_lock():
try:, os.O_CREAT | os.O_EXLOCK | os.O_NDELAY)
except OSError:
sys.exit('Could not acquire lock.')
def pages2mb(page_count):
return int(page_count) * 4096 / 1024 ** 2
def free_inactive():
vmstat = Popen('vm_stat', shell=True, stdout=PIPE)
inactive = pages2mb(
free = pages2mb( + \
return free, inactive
def main():
free, inactive = free_inactive()
if (free < FREE_THRESHOLD) and (inactive > INACTIVE_THRESHOLD):
print("Free: %dmb < %dmb" % (free, FREE_THRESHOLD))
print("Inactive: %dmb > %dmb" % (inactive, INACTIVE_THRESHOLD))
call('/usr/bin/purge', shell=True)
if __name__ == '__main__':

gsong commented Mar 17, 2012

  1. Make sure is executable: chmod +x.
  2. Adjust the inactive and free memory thresholds on lines 9 and 10 of The setting here is for an 8GB machine.
  3. Modify the path of accordingly in line 9 of net.damacy.releasemem.plist.
  4. Adjust the frequency for the job by adjusting line 12 of net.damacy.releasemem.plist. Here it's set for every 60 seconds.
  5. Save or symlink net.damacy.releasemem.plist to $HOME/Library/LaunchAgents.
  6. launchctl load -w $HOME/Library/LaunchAgents/net.damacy.releasemem.plist.

maybe a more user friendly tut would be an idea, for instance where do we put it? your path points to a .bin yet with files unhidden i see no bin file other than in root, does its location matter?


gsong commented Feb 15, 2013

There's a blog post to go with this gist.

You can put anywhere, as long as you point to the correct location in the launchd plist. In my case, I created .bin in my home directory.

Thanks buddy. This script seems to be working just fine on my Mac Pro with Lion and 8GB :) Appreciate the effort.

Need your help. I'm using Mountain Lion but with no luck, also have made the to be executable. But I saw the message in the console "failed to exec(3) for weird reason: 13". Any advice?

------------ Updates ------------

Changed "#!/usr/bin/env python" to "#!/usr/bin/python". It works! Nice code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment