Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > OS X

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 14, 2012, 01:49 PM   #1
RedCroissant
macrumors 68000
 
RedCroissant's Avatar
 
Join Date: Aug 2011
Location: Arizona
Automate "purge" command?

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.
__________________
Welcome to the Grid, Program.

27" iMac, 3.2GHz i5, 16GB RAM, 3TB HDD; 32GB iPad 1 WiFi+3G; 30GB iPod Video 5G
RedCroissant is offline   0 Reply With Quote
Old Dec 14, 2012, 02:26 PM   #2
meme1255
macrumors 6502
 
Join Date: Jul 2012
Location: Czech Republic
I'll add another parts when I'll find something that works:

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 ;
top -l 1 | grep PhysMem: | awk -F, '{$10 > 500} END {print "I'm taking too much memory "} '
__________________
MacBook Pro 2008 Late 2,53 GHz, MBA 2012, iPad 2 iOS 7.1.1 64GB 3G, iPhone 4S 16GB iOS 7.1.1
PSPad for Mac Cider games [ C&C 3 etc.]flicker fix

Last edited by meme1255; Dec 14, 2012 at 02:48 PM.
meme1255 is offline   0 Reply With Quote
Old Dec 14, 2012, 02:40 PM   #3
RedCroissant
Thread Starter
macrumors 68000
 
RedCroissant's Avatar
 
Join Date: Aug 2011
Location: Arizona
Quote:
Originally Posted by meme1255 View Post
I'll add another parts when I'll find something that works:

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!
__________________
Welcome to the Grid, Program.

27" iMac, 3.2GHz i5, 16GB RAM, 3TB HDD; 32GB iPad 1 WiFi+3G; 30GB iPod Video 5G
RedCroissant is offline   0 Reply With Quote
Old Dec 14, 2012, 02:50 PM   #4
meme1255
macrumors 6502
 
Join Date: Jul 2012
Location: Czech Republic
But that Automator workflow isn't cleaning automatically, is it?
__________________
MacBook Pro 2008 Late 2,53 GHz, MBA 2012, iPad 2 iOS 7.1.1 64GB 3G, iPhone 4S 16GB iOS 7.1.1
PSPad for Mac Cider games [ C&C 3 etc.]flicker fix
meme1255 is offline   0 Reply With Quote
Old Dec 14, 2012, 03:08 PM   #5
meme1255
macrumors 6502
 
Join Date: Jul 2012
Location: Czech Republic
I'm proceeding forward..

Quote:
#!/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) )
__________________
MacBook Pro 2008 Late 2,53 GHz, MBA 2012, iPad 2 iOS 7.1.1 64GB 3G, iPhone 4S 16GB iOS 7.1.1
PSPad for Mac Cider games [ C&C 3 etc.]flicker fix

Last edited by meme1255; Dec 14, 2012 at 03:32 PM.
meme1255 is offline   0 Reply With Quote
Old Dec 14, 2012, 10:43 PM   #6
RedCroissant
Thread Starter
macrumors 68000
 
RedCroissant's Avatar
 
Join Date: Aug 2011
Location: Arizona
Quote:
Originally Posted by meme1255 View Post
But that Automator workflow isn't cleaning automatically, is it?
Quote:
Originally Posted by meme1255 View Post
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.
__________________
Welcome to the Grid, Program.

27" iMac, 3.2GHz i5, 16GB RAM, 3TB HDD; 32GB iPad 1 WiFi+3G; 30GB iPod Video 5G
RedCroissant is offline   0 Reply With Quote
Old Dec 15, 2012, 07:54 AM   #7
switon
macrumors 6502a
 
Join Date: Sep 2012
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 by switon; Dec 15, 2012 at 01:14 PM.
switon is offline   2 Reply With Quote
Old Dec 15, 2012, 08:08 AM   #8
benwiggy
macrumors 68020
 
Join Date: Jun 2012
Quote:
Originally Posted by switon View Post
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.
benwiggy is offline   2 Reply With Quote
Old Dec 15, 2012, 08:13 AM   #9
switon
macrumors 6502a
 
Join Date: Sep 2012
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 by switon; Dec 15, 2012 at 08:33 AM.
switon is offline   1 Reply With Quote
Old Dec 15, 2012, 08:59 AM   #10
meme1255
macrumors 6502
 
Join Date: Jul 2012
Location: Czech Republic
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
__________________
MacBook Pro 2008 Late 2,53 GHz, MBA 2012, iPad 2 iOS 7.1.1 64GB 3G, iPhone 4S 16GB iOS 7.1.1
PSPad for Mac Cider games [ C&C 3 etc.]flicker fix
meme1255 is offline   0 Reply With Quote
Old Dec 15, 2012, 09:14 AM   #11
switon
macrumors 6502a
 
Join Date: Sep 2012
RE: automator...scripts...

Quote:
Originally Posted by meme1255 View Post
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 is offline   0 Reply With Quote
Old Dec 15, 2012, 09:37 AM   #12
switon
macrumors 6502a
 
Join Date: Sep 2012
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.
switon is offline   0 Reply With Quote
Old Dec 15, 2012, 09:42 AM   #13
RedCroissant
Thread Starter
macrumors 68000
 
RedCroissant's Avatar
 
Join Date: Aug 2011
Location: Arizona
Quote:
Originally Posted by switon View Post
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.
__________________
Welcome to the Grid, Program.

27" iMac, 3.2GHz i5, 16GB RAM, 3TB HDD; 32GB iPad 1 WiFi+3G; 30GB iPod Video 5G
RedCroissant is offline   0 Reply With Quote
Old Dec 15, 2012, 09:53 AM   #14
switon
macrumors 6502a
 
Join Date: Sep 2012
RE: inactive and VirtualBox...

Quote:
Originally Posted by RedCroissant View Post
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
switon is offline   0 Reply With Quote
Old Dec 15, 2012, 10:23 AM   #15
RedCroissant
Thread Starter
macrumors 68000
 
RedCroissant's Avatar
 
Join Date: Aug 2011
Location: Arizona
Quote:
Originally Posted by switon View Post
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.
__________________
Welcome to the Grid, Program.

27" iMac, 3.2GHz i5, 16GB RAM, 3TB HDD; 32GB iPad 1 WiFi+3G; 30GB iPod Video 5G

Last edited by RedCroissant; Dec 15, 2012 at 10:29 AM.
RedCroissant is offline   0 Reply With Quote
Old Dec 15, 2012, 10:28 AM   #16
switon
macrumors 6502a
 
Join Date: Sep 2012
RE: VB and memory leaks and ...

Quote:
Originally Posted by RedCroissant View Post
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
switon is offline   0 Reply With Quote
Old Dec 15, 2012, 10:37 AM   #17
RedCroissant
Thread Starter
macrumors 68000
 
RedCroissant's Avatar
 
Join Date: Aug 2011
Location: Arizona
Quote:
Originally Posted by switon View Post
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.
__________________
Welcome to the Grid, Program.

27" iMac, 3.2GHz i5, 16GB RAM, 3TB HDD; 32GB iPad 1 WiFi+3G; 30GB iPod Video 5G
RedCroissant is offline   0 Reply With Quote
Old Dec 15, 2012, 10:42 AM   #18
switon
macrumors 6502a
 
Join Date: Sep 2012
RE: that shell script...

Quote:
Originally Posted by RedCroissant View Post
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
switon is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > OS X

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
General: iOS7 command line... Equivalent of "say" command? RobertoG Jailbreaks and iOS Hacks 0 Jan 19, 2014 05:05 PM
How do I get the "purge" command? RedCroissant PowerPC Macs 8 Jan 18, 2014 06:33 PM
Purge Command Broken 10.8.1 isrefel OS X 10.8 Mountain Lion 42 Oct 28, 2013 02:18 AM
"purge" Command !!! OS X Mavericks (10.9) 10 Aug 11, 2013 10:22 AM
"Applications" menu command - "item cannot be found" - owner/permissions? (10.6.8) MacOSExustio OS X 2 Jul 20, 2013 05:05 AM

Forum Jump

All times are GMT -5. The time now is 08:53 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC