Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

RedCroissant

Suspended
Original poster
Aug 13, 2011
2,268
96
Is there a way to automate terminal running the "purge" command when inactive memory reaches a certain level? I know it's not a huge hassle to open up terminal and do it that way, but I would prefer that it just happens automatically when inactive memory reaches 500mb. Thanks.
 

meme1255

macrumors 6502a
Jul 15, 2012
742
594
Czechia
I'll add another parts when I'll find something that works: :D

For now, I'd suggest automating this command somehow:
top -l 1 | grep PhysMem: | awk '{print $10}'

And if variable_$10" > 500 then purge..
I'll try to make that somehow

First edit; work and non-working-cop of bash script :D :( ;
top -l 1 | grep PhysMem: | awk -F, '{$10 > 500} END {print "I'm taking too much memory "} '
 
Last edited:

RedCroissant

Suspended
Original poster
Aug 13, 2011
2,268
96
I'll add another parts when I'll find something that works: :D

For now, I'd suggest automating this command somehow:
top -l 1 | grep PhysMem: | awk '{print $10}'

And if variable_$10" > 500 then purge..
I'll try to make that somehow

Thanks! I did an initial automator with the basic "purge" command in a shell script and then saved as an application on my desktop(and it works pretty well), I just wanted it simpler; so thanks!
 

meme1255

macrumors 6502a
Jul 15, 2012
742
594
Czechia
I'm proceeding forward..

#!/bin/bash
N = "100M"
memory=$(top -l 1 | grep PhysMem: | awk '{print $6}' )
if [$N""*< "$memory" ]; then
echo "It taking too much memory"
else
echo "It is okay"
fi
M in variable is a problem :/

---> Find the way how to remove "M" from the grep result and it should work ( instead echo memory use do "purge" (I'm not sure with syntax, sorry) )
 
Last edited:

RedCroissant

Suspended
Original poster
Aug 13, 2011
2,268
96
But that Automator workflow isn't cleaning automatically, is it?

I'm proceeding forward..


M in variable is a problem :/

---> Find the way how to remove "M" from the grep result and it should work ( instead echo memory use do "purge" (I'm not sure with syntax, sorry) )

No it isn't, and I have to say that I have no idea what the hell to do since I know absolutely nothing about coding or terminal to that extent. I've done a tiny bit of HTML and have played around with terminal by copying and pasting commands, but nothing like this and I am utterly lost.
 

switon

macrumors 6502a
Sep 10, 2012
636
1
RE: bash scripts...string versus arithmetic values...

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 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
 
Last edited:

benwiggy

macrumors 68020
Jun 15, 2012
2,382
198
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.
Quoted for truth.
Unless you are experiencing NOTICEABLE performance issues (i.e. noticeable when you're not looking at Activity Monitor), then there is absolutely no need to purge memory.
I've seen loads of threads here with worries about the amount of RAM that ML is using; I've not seen any that point to any actual problems as a result.
 

switon

macrumors 6502a
Sep 10, 2012
636
1
RE: that purge...

Hi benwiggy,

Absolutely! In fact, if you purge then in general you will get poorer performance for the typical usage since the OS will have to reload the program into memory the next time you rerun it.

I have only ever used the purge command for code testing purposes.

Thanks for the verification,
Switon
 
Last edited:

meme1255

macrumors 6502a
Jul 15, 2012
742
594
Czechia
And if you wrap it all into while(1), it could probably run automatically (I'm not sure ) ;), but I think that wouldn't help you much :)
 

switon

macrumors 6502a
Sep 10, 2012
636
1
RE: automator...scripts...

And if you wrap it all into while(1), it could probably run automatically (I'm not sure ) ;), but I think that wouldn't help you much :)

There are several ways to make the script run automatically, as I'm sure you know. You could, as you suggest, run the script from your login .bashrc file and have it running always. Or you could use Automator to generate a automator application to run this script continuously.

But then again, as I and benwiggy pointed out, you really wouldn't want to do "purge"s continuously, since your performance would then decrease not increase. There is a reason for inactive memory, and it is based upon how a typical user uses his/her machine. Typically, a user will jump between applications, and if the application is already in inactive memory, then this jumping will happen very quickly as the application is already in RAM. But if you purge the inactive memory, then when jumping to a previous app the OS will have to load the app into RAM from the HDD, something that takes a much longer time.

Switon
 

switon

macrumors 6502a
Sep 10, 2012
636
1
RE: ...précis...

Hi,

Allow me to quickly summarize:

(1) There is an important distinction between lexicographical and arithmetic comparisons in bash, thus lexicographically the string "6" is always greater than the string "59999", while arithmetically the value 6 is less than the value 59999.

This means that one has to be careful if you are comparing strings versus comparing numerical values. The string comparisons will not necessarily give you what you might expect.

(2) If you are attempting to improve your computer's performance by doing "purge"s, then you are actually more likely to be decreasing performance, not increasing performance.

Inactive memory serves a useful purpose for the OS. RAM memory that is not actually being used is superfluous to performance --- if you have 1000GB of RAM, but your applications only use 10GB, then the extra 990GB of RAM does you no good, it does not improve performance. So the Mac OS X attempts to make use of the extra unused RAM by marking it as "inactive" when a program has finished with it instead of erasing its contents. If the system runs out of free RAM, then the OS will recover memory from the inactive portion. But there is always a chance that the user will rerun an app that is already in inactive memory, and when this happens the OS does not have to reload the app from the HDD into RAM (this takes time), rather it just makes the inactive RAM active again.

Regards,
Switon

P.S. I have only ever used the "purge" command when testing code, never for performance reasons.
 

RedCroissant

Suspended
Original poster
Aug 13, 2011
2,268
96
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

Thank you very much for this! This is awesome and I'm going to try it out today. And the reason I want to continually purge inactive memory is mainly due to my wanting the memory freed up faster. I find that when running VirtualBox or Handbrake, that they both consume a lot of RAM and the system I slow to release it after that. I have seen my 8GB RAM go from 300mb inactive all the way up to 5.3GB. At that point, I notice that the system becomes sluggish and usually end up opening terminal and performing the purge command.
 

switon

macrumors 6502a
Sep 10, 2012
636
1
RE: inactive and VirtualBox...

Thank you very much for this! This is awesome and I'm going to try it out today. And the reason I want to continually purge inactive memory is mainly due to my wanting the memory freed up faster. I find that when running VirtualBox or Handbrake, that they both consume a lot of RAM and the system I slow to release it after that. I have seen my 8GB RAM go from 300mb inactive all the way up to 5.3GB. At that point, I notice that the system becomes sluggish and usually end up opening terminal and performing the purge command.

Wow! So let me verify: you incur a slowing that you attribute to the inactive RAM taking over, but after you "purge" in order to free up this RAM then your system is more responsive?

I wonder if this is somehow due to the way VirtualBox works? Maybe it is slowing how Mac OS X recovers inactive memory?! I have to say that I have never seen this myself, as I find the Mac OS X does a good and efficient and fast job of recovering inactive memory when required. I also do not use VirtualBox...

...just my experience...

Regards,
Switon
 

RedCroissant

Suspended
Original poster
Aug 13, 2011
2,268
96
Wow! So let me verify: you incur a slowing that you attribute to the inactive RAM taking over, but after you "purge" in order to free up this RAM then your system is more responsive?

I wonder if this is somehow due to the way VirtualBox works? Maybe it is slowing how Mac OS X recovers inactive memory?! I have to say that I have never seen this myself, as I find the Mac OS X does a good and efficient and fast job of recovering inactive memory when required. I also do not use VirtualBox...

...just my experience...

Regards,
Switon

Yes. When I use VB, I allocate a lot of the system resources to the virtual machine because I like to have a lose to a native OS experience as possible, especially when my class requires me to use Windows and Internet Explorer. So due to my, what might be considered an over-allocation of resources, after I am finished with VB, my system is slow and I start using swap and getting page-outs.

And something that I have found interests is that when using Windows in a virtual machine, my inactive memory increases more rapidly than when using Linux. The Windows OS also usually tries to utilize more RAM than I initially assign to it(but then that might be a side effect of Vista as well).

Handbrake does the same tang for me, but not as extreme as VB, and the reason I really liked that fact that you write that script is because I shouldn't have to en up terminal to do the purge command. It's great.
 
Last edited:

switon

macrumors 6502a
Sep 10, 2012
636
1
RE: VB and memory leaks and ...

Yes. When I use VB, I allocate a lot of the system resources to the virtual machine because I like to have a lose to a native OS experience as possible, especially when my class requires me to use Windows and Internet Explorer. So due to my, what might be considered an over-allocation of resources, after I am finished with VB, my system is slow and I start using swap and getting page-outs.

Ahah, I suspect that VB is not freeing the memory correctly when you finish with it! This is not a problem with the Mac OS X not recovering the inactive RAM properly, rather this is a problem with VirtualBox not freeing the memory correctly!? Maybe there are memory leaks in VB that it then does not handle correctly when terminating? What do you think?

Switon
 

RedCroissant

Suspended
Original poster
Aug 13, 2011
2,268
96
Ahah, I suspect that VB is not freeing the memory correctly when you finish with it! This is not a problem with the Mac OS X not recovering the inactive RAM properly, rather this is a problem with VirtualBox not freeing the memory correctly!? Maybe there are memory leaks in VB that it then does not handle correctly when terminating? What do you think?

Switon

That's what I originally ought, but don't have the actual knowledge to make that determination. The other issue is that I have to share the video memory with the virtual machine and (I think) is another good reason to do the purge.
 

switon

macrumors 6502a
Sep 10, 2012
636
1
RE: that shell script...

And something that I have found interests is that when using Windows in a virtual machine, my inactive memory increases more rapidly than when using Linux. The Windows OS also usually tries to utilize more RAM than I initially assign to it(but then that might be a side effect of Vista as well).

Handbrake does the same tang for me, but not as extreme as VB, and the reason I really liked that fact that you write that script is because I shouldn't have to en up terminal to do the purge command. It's great.

Humm...this partially confirms my contention that there are memory leaks by VB and Vista that are not being handled properly.

And, in regards to that script...of course you can "comment out" the "echo"es so that they won't print to your console when the script is executed. Hope you find it useful.

Switon
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.