-
-
Save gsong/2063616 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>Label</key> | |
<string>net.damacy.releasemem</string> | |
<key>ProgramArguments</key> | |
<array> | |
<string>/Users/george/.bin/releasemem.py</string> | |
</array> | |
<key>StartInterval</key> | |
<integer>60</integer> | |
<key>Nice</key> | |
<integer>20</integer> | |
<key>LowPriorityIO</key> | |
<true/> | |
</dict> | |
</plist> |
#!/usr/bin/env python | |
import os | |
import re | |
import sys | |
from subprocess import call, Popen, PIPE | |
INACTIVE_THRESHOLD = 1024 # Number of MBs | |
FREE_THRESHOLD = INACTIVE_THRESHOLD / 2 | |
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.open(LOCK_FILE, 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).stdout.read() | |
inactive = pages2mb(RE_INACTIVE.search(vmstat).group(1)) | |
free = pages2mb(RE_FREE.search(vmstat).group(1)) + \ | |
pages2mb(RE_SPECULATIVE.search(vmstat).group(1)) | |
return free, inactive | |
def main(): | |
acquire_lock() | |
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)) | |
print('Purging...') | |
call('/usr/bin/purge', shell=True) | |
if __name__ == '__main__': | |
main() |
maybe a more user friendly tut would be an idea, for instance releasemem.py 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?
There's a blog post to go with this gist.
You can put releasemem.py
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 releasemem.py 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!
releasemem.py
is executable:chmod +x
.releasemem.py
. The setting here is for an 8GB machine.releasemem.py
accordingly in line 9 ofnet.damacy.releasemem.plist
.net.damacy.releasemem.plist
. Here it's set for every 60 seconds.net.damacy.releasemem.plist
to$HOME/Library/LaunchAgents
.launchctl load -w $HOME/Library/LaunchAgents/net.damacy.releasemem.plist
.