Monday, November 28, 2011

Recover Memory On Mac OS X Lion For Free

Using the technique below I noticed significant gains, you can verify them aswell by opening the Activity Monitor.app, right clicking the icon in the Dock and setting it to show the Memory.
Keep an eye on the green part of the pie chart when the script runs.

First, select the text editor of your choice, I personally use Textmate, and create a new file:

mate ~/Library/LaunchAgents/org.itweak.purge.plist

For the bash novices the tilde (~) sign indicates your home directory, usually /Users/yourname

Copy/paste this code:

<?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>org.itweak.purge</string>
  <key>Program</key>
  <string>/usr/bin/purge</string>
  <key>StartInterval</key>
  <integer>3600</integer>
</dict>
</plist>

Save the file (CMD+S will suffice) and close the editor.

What we just did is to build a plist file that instructs our system to run the value of the Program key (/usr/bin/purge) every 3600 seconds.

Open up a Terminal and write this:

launchctl load -w ~/Library/LaunchAgents/org.itweak.purge.plist

Now, this job will run every hour, you can tweak the number of seconds by changing the StartInterval integer number (in seconds) and it will free up your memory, you said it, for free.

To stop it:

launchctl unload -w ~/Library/LaunchAgents/org.itweak.purge.plist

A downside of this is that the purge command will kinda freeze your mac for few seconds when it runs. It can be annoying at times.

No comments: