Hi RedCroissant and meme1255,
I hope you don't mind if I jump in here? (meme1255, you were close in your code, except for one small detail...)
The following bash script works to do what you wish to do. (Now there are many ways of achieving your goal, this is just one way.) I attempted to comment the script to describe what it does. The chief thing to remember here is that strings are compared differently than arithmetic values. For instance, the string "6" is always lexicographically greater than the string "599", while the arithmetic value 6 is less than 599. The following bash code extracts the inactive memory as a string, then deletes the M at the back of the string, then compares the arithmetic values and if inactive memory is too large it performs a purge. By the way, the 'spaces' (or lack thereof) in the code are important, do not delete them (or add new ones). This script file is named "purge.sh".
Code:
#!/bin/bash
# purge.sh:
# This bash script will compare the amount of inactive
# memory to a specified value. If the amount of inactive
# memory is lower, nothing is done, if it is greater then
# a purge is performed. -- Switon
#
# Setup the threshold for purging,
# any inactive memory above this will cause a purge:
# (Note that the second assignment is commented out, i.e.,
# it has a pound # character before it. If you remove
# the # character then you can test this routine further.)
NN="100M"
#NN="5000M"
# Get the amount of amount of inactive memory:
memory=$( top -l 1 | grep PhysMem: | awk '{print $6}' )
# Remove the M at the back of these strings:
NNN=${NN%%M}
inactive=${memory%%M}
# Echo the strings:
echo "\$NN: $NN , \$memory: $memory"
echo "\$NNN: $NNN , \$inactive: $inactive"
# Check if the arithmetic value of $NNN is less than the
# arithmetic value of $inactive:
if [ $((NNN)) -lt $((inactive)) ]; then
echo "Too much inactive memory: $NNN < $inactive"
echo "Performing the purge! "; purge
else
echo "Inactive memory is okay: $NNN > $inactive"
echo "Not doing a purge."
fi
Below are example executions of this bash script, first when NN is defined as 100M:
(note that I had commented out the actual purge command so I did not perform the purge when executing this script)
Code:
switon$ sh purge.sh
$NN: 100M , $memory: 1640M
$NNN: 100 , $inactive: 1640
Too much inactive memory: 100 < 1640
Performing the purge!
...now when NN is defined as 5000M:
Code:
switon$ sh purge.sh
$NN: 5000M , $memory: 1642M
$NNN: 5000 , $inactive: 1642
Inactive memory is okay: 5000 > 1642
Not doing a purge.
Okay, so here is a bash shell script that does your purge. If you would like to learn bash programming, I would recommend the "Learning the bash shell" O'Reilly book.
Now I have to ask, why do you want to do the purge? The Mac OS X already takes care of this for you, so the above purge is superfluous to a running system. When free memory is exhausted, the Mac OS X will free up the inactive memory when more memory is needed. But until it is needed, the inactive memory remains --- and one would argue that this is just what you want to happen, since if you go back and rerun something that is in inactive memory, the OS does not have to reload it so it runs faster the second time.
Regards,
Switon