03. Release inactive memory
This task checks system memory usage repeatedly and releases inactive memory if it's necessary.

Original Article(Japanese)

Please note that the author takes no responsibility for any damage caused by the use of those scripts.

(Trigger)
1. Repeat
Set interval like 15 minutes, 30 minutes or 1 hour as you want.




(Action)
1. Run Applescript
Input following script.
property rt : return as text on run tell application "Finder" activate set alertResult to display alert "【 Memory status 】" & rt & "Free memory space is not much now." & rt & "Release inactive memory?" message my memStatus() buttons {"Cancel", "Run"} default button 2 --giving up after 120 if alertResult is {button returned:"Run"} then do shell script "du -s / &> /dev/null & sleep 10 && kill $!" delay 2 tell application "GrowlHelperApp" to notify with name "Task3MessageWorkNotification" title (("Inactive memory is released" & rt & "Current memory status") as Unicode text) description (my memStatus() as Unicode text) application name "task3runner" icon of application "task3runner" sticky "yes" end if end tell end run on memStatus() set memoryStatus to do shell script "top -l 1 | head -10 | grep PhysMem" set wiredMem to word 2 of memoryStatus set activeMem to word 4 of memoryStatus set inactiveMem to word 6 of memoryStatus set usedMem to word 8 of memoryStatus set freeMem to word 10 of memoryStatus set swapusage to do shell script "sysctl vm.swapusage" set theSwap to "Swap : " & word 7 of swapusage & " (" & word 4 of swapusage & ")" set grepLine to do shell script "top -l 1 | head -10 | grep VM" set pageins to (((round (word 7 of grepLine as number) / 4096 * 100) / 100 * 16) as text) & "M" set pageouts to (((round (word 10 of grepLine as number) / 4096 * 100) / 100 * 16) as text) & "M" return "Free: " & freeMem & rt & "Wired: " & wiredMem & rt & "Active: " & activeMem & rt & "Inactive: " & inactiveMem & rt & "Used: " & usedMem & rt & rt & theSwap & rt & "Page Ins : " & pageins & rt & "Page Outs : " & pageouts & rt end memStatus

When this script is run, the current memory usage status will be shown in a dialog which has "Run" and "Cancel" buttons.



If "Run" button is pressed, following shell script is executed to release inactive memory.
du -s / &> /dev/null & sleep 10 && kill $!
After running this shell script, the memory status will be displayed again with Growl at this time.




(Condition)
1. AppleScript
Input following script to judge whether the memory should be released or not.
set limitMem to 1000 -- If free memory size is less than this number (MB), the action will be run. -- with this example, if free memory size is less than 1,000MB, Task3 asks to release memory. set freeMem to (do shell script "echo " & word 10 of (do shell script "top -l 1 | head -10 |grep PhysMem") & " | sed -e 's/M//g'") as number return freeMem ≤ limitMem

To set a script as condition, the script should return boolean value, true or false. With this sample, the script returns false value if free memory is larger than 1,000MB, otherwise returns true. If the script returns true, the actions of the task are executed.


 

To Top and Map