Go Back   The macosxhints Forums > OS X Help Requests > Applications



Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 5.00 average. Display Modes
Old 12-14-2006, 07:01 AM   #1
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
AppleScript to display Safari's memory usage

As many of you will have noticed, Safari often seems to get itself into a state where it starts to take a lot of memory. I'm not yet convinced that this is technically a "memory leak" (where an app has lost track of memory that it allocated) since it might just be that Safari is misguidedly caching too much info about previously viewed pages or whatever.

But it certainly is the case that at times Safari starts to take much more of my precious RAM than I want it to. I don't mind too much if it is taking 100 MB if I've been looking at graphically-rich pages, but I expect it to be a lot less than that most of the time. And if I notice that Safari is taking up much more than 100 MB of real memory (e.g. by looking at Activity Monitor), I will quit and re-launch it in order to conserve RAM.

This post presents an AppleScript that monitors the RAM usage of Safari and displays it (in megabytes) in the title of the frontmost window.
The AppleScript pops up an alert dialog if the Safari memory usage exceeds 150 MB.
There are two versions of this AppleScript:
  • one that you compile (in Script Editor) as an application and run by double-clicking (I'll call this VersionA)
  • one that is embedded in a Perl script and which you run from the command-line in a Terminal window (I'll call this VersionP)
The advantage of the first version is that it doesn't require a visit to the Terminal.
However I noticed that the first version takes up more than 10 MB of RAM all the time it is running.
The second version takes up far less RAM - less than 1 MB of RAM most of the time and 2 or 3 MB of RAM during the short times that the embedded AppleScript runs (every 10 seconds).
Neither version takes significant amounts of CPU - the first version averages about 0.2 % of the CPU on my iBook 1.2 GHz G4. The second version takes too small a percentage of CPU to measure.

Of course you would only use one of these two versions at a time.

Here's the VersionA script:
Code:
-- This AppleScript displays the amount of memory being used by Safari
-- in the title of the frontmost Safari window.
-- It is intended to be saved as an application
-- and left running all of the time.
-- Cameron Hayne (macdev@hayne.net)  December 2006

-- getProcessMB:
-- Returns the number of megabytes used by the specified process.
-- It gets this value by invoking the Unix 'ps' command via a Perl script.
on getProcessMB(procName)
	set perlCode to "
# This script gets the 'RSS' of the process specified as 'procName'
# The 'RSS' (resident set size) is a rough measure of how much RAM
# the process is using. The value is given in megabytes.
open(PS, \"/bin/ps -xc -o command,rss |\");
while (<PS>)
{
    if (/^\\s*" & procName & "\\s+(\\d+)\\s*$/o)
    {
        my $rss = $1;
        my $rssMB = sprintf(\"%.1f\", $rss / 1024);
        print $rssMB;
        last;
    }
}
close(PS);
"
	set numMB to do shell script "perl -e " & quoted form of perlCode
	return numMB as real
end getProcessMB

-- removeLastPart:
-- Returns the string 'str' without the last part starting with 'delim'
on removeLastPart(str, delim)
	set saveDelim to AppleScript's text item delimiters
	set AppleScript's text item delimiters to delim
	if (count str's text items) > 1 then
		set str to str's text 1 thru text item -2
	end if
	set AppleScript's text item delimiters to saveDelim
	return str
end removeLastPart

-- appIsRunning:
-- Returns true if the app 'appName' is running, otherwise it returns false
on appIsRunning(appName)
	set isRunning to false
	tell application "System Events"
		if (count (every process whose name is appName)) > 0 then
			set isRunning to true
		end if
	end tell
	return isRunning
end appIsRunning

-- showAppSizeInTitle:
-- Displays the amount of memory being used by the app 'appName' in the title
-- of the frontmost window of that app.
-- It pops up an alert dialog if the amount of memory used
-- is greater than 'warnMB' (megabytes).                
on showAppSizeInTitle(appName, warnMB)
	if my appIsRunning(appName) then
		tell application appName
			set title to name of window 1
			set delim to " *** "
			set origTitle to my removeLastPart(title, delim)
			set numMB to my getProcessMB(appName)
			set title to origTitle & delim & numMB & " MB"
			set name of window 1 to title
			
			if numMB is greater than warnMB then
				display alert appName & " is taking more than " & warnMB & " MB"
			end if
		end tell
	end if
end showAppSizeInTitle

on idle
	showAppSizeInTitle("Safari", 150)
	return 10 -- updates every 10 seconds
end idle
To use the above version, copy & paste the code to Script Editor, then Save as an application bundle (.app). Then double-click to launch the app as with any other application.

Here's the VersionP script:
Code:
#!/usr/bin/perl

# safariSizeInTitle:
# This script displays the amount of memory being used by Safari
# in the title of the frontmost Safari window.
# It will pop up an alert dialog if Safari's memory consumption exceeds $warnMB
# It is intended to be left running all of the time in a Terminal window.
# You can put it in the background if you want.
#
# Cameron Hayne (macdev@hayne.net)  December 2006

use strict;
use warnings;

my $appName = "Safari";
my $warnMB = 150; # memory usage (in megabytes) at which an alert is given
my $period = 10;  # time (in seconds) between updates

# function declarations:
sub runAppleScript($$);
sub getProcessMB($);
sub showAppSizeInTitle($$);

MAIN:
{
    while (1)
    {
        showAppSizeInTitle($appName, $warnMB);
        sleep $period;
    }
}

# runAppleScript: Runs the supplied AppleScript
#                 The arguments to this function are:
#                 - the text of the AppleScript
#                 - the command-line args to be passed to the AppleScript
sub runAppleScript($$)
{
    my ($ascript, $args) = @_;

    my $result = `/usr/bin/osascript - $args<<"    EOT"
    $ascript
    EOT
    `;
    chomp($result);
    return $result;
}

# getProcessMB: returns the number of megabytes used by the specified process
#               returns 'undef' if there is no such process
sub getProcessMB($)
{
    my ($procName) = @_;

    my $rssMB;
    my $psCmd = "/bin/ps -xc -o command,rss";
    open(PS, "$psCmd |") or die "Can't run command \"$psCmd\": $!\n";
    while (<PS>)
    {
        if (/^\s*$procName\s+(\d+)\s*$/o)
        {
            my $rss = $1;
            $rssMB = sprintf("%.1f", $rss / 1024);
            last;
        }
    }
    close(PS);

    return $rssMB;
}

# showAppSizeInTitle:
# Displays the amount of memory being used by the app 'appName' in the title
# of the frontmost window of that app.
# It pops up an alert dialog if the amount of memory used
# is greater than 'warnMB' (megabytes).                
sub showAppSizeInTitle($$)
{
    my ($appName, $warnMB) = @_;

    my $numMB = getProcessMB($appName);
    return unless defined($numMB);

    my $applescript = <<"    EOT";
    -- removeLastPart:
    -- Returns the string 'str' without the last part starting with 'delim'
    on removeLastPart(str, delim)
        set saveDelim to AppleScript's text item delimiters
        set AppleScript's text item delimiters to delim
        if (count str's text items) > 1 then
            set str to str's text 1 thru text item -2
        end if
        set AppleScript's text item delimiters to saveDelim
        return str
    end removeLastPart

    tell application "$appName"
        set title to name of window 1
        set delim to " *** "
        set origTitle to my removeLastPart(title, delim)
        set title to origTitle & delim & $numMB & " MB"
        set name of window 1 to title

        if $numMB is greater than $warnMB then
            display alert "$appName is taking more than $warnMB MB"
        end if
    end tell
    EOT

    runAppleScript($applescript, "");
}
To use this version, copy & paste the code into a plain text file, being sure that the line-endings are set to Unix, make that file executable with 'chmod +x', and then run it by typing the name of the script file on the Terminal command line. See the "running scripts" section of this Unix FAQ for details.

I'm interested in hearing what others think of this script (either version) since I will probably submit this as a "hint" on the main macosxhints site and it is best to get the bugs out ahead of time. Of course any suggestions for improvement (either of functionality or implementation) are welcome.
__________________
hayne.net/macosx.html

Last edited by hayne; 12-14-2006 at 02:56 PM.
hayne is offline   Reply With Quote
Old 12-14-2006, 11:18 AM   #2
kainewynd2
Triple-A Player
 
Join Date: Nov 2005
Location: Eastern US
Posts: 138
A simple copy paste of the first code to Applescript doesn't seem to accomplish anything - no errors and no response.

The perl script gives the following output when attempting to run:
2006-12-14 10:16:30.034 osascript[576] CFLog (21): dyld returns 2 when trying to load /Users/mjw1/Library/ScriptingAdditions/24U Appearance OSAX.osax/Contents/MacOS/24U Appearance OSAX
2006-12-14 10:16:30.052 osascript[576] CFLog (21): dyld returns 2 when trying to load /Users/mjw1/Library/ScriptingAdditions/24U Appearance OSAX.osax/Contents/MacOS/24U Appearance OSAX
2006-12-14 10:16:40.296 osascript[579] CFLog (21): dyld returns 2 when trying to load /Users/mjw1/Library/ScriptingAdditions/24U Appearance OSAX.osax/Contents/MacOS/24U Appearance OSAX
2006-12-14 10:16:40.313 osascript[579] CFLog (21): dyld returns 2 when trying to load /Users/mjw1/Library/ScriptingAdditions/24U Appearance OSAX.osax/Contents/MacOS/24U Appearance OSAX
2006-12-14 10:16:50.554 osascript[582] CFLog (21): dyld returns 2 when trying to load /Users/mjw1/Library/ScriptingAdditions/24U Appearance OSAX.osax/Contents/MacOS/24U Appearance OSAX
2006-12-14 10:16:50.569 osascript[582] CFLog (21): dyld returns 2 when trying to load /Users/mjw1/Library/ScriptingAdditions/24U Appearance OSAX.osax/Contents/MacOS/24U Appearance OSAX
2006-12-14 10:17:00.841 osascript[586] CFLog (21): dyld returns 2 when trying to load /Users/mjw1/Library/ScriptingAdditions/24U Appearance OSAX.osax/Contents/MacOS/24U Appearance OSAX
2006-12-14 10:17:00.859 osascript[586] CFLog (21): dyld returns 2 when trying to load /Users/mjw1/Library/ScriptingAdditions/24U Appearance OSAX.osax/Contents/MacOS/24U Appearance OSAX

I canceled the process after these many popped up. Is that by design?
kainewynd2 is offline   Reply With Quote
Old 12-14-2006, 11:46 AM   #3
NovaScotian
League Commissioner
 
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 5,155
The first script fails on mm with the message: "Result of numeric operation was too large" - I think because of a divide by zero somewhere, possibly.

As I go through the handlers, on mm the first: "getProcessMB" produces a null result. The AppleScript below works for me in its place (I haven't examined the others yet):

to getProcMB(procName) -- as string
try
return ((last word of (do shell script "/bin/ps -xc -o command,rss | grep " & procName)) as integer) / 1024 -- result of a division will be real
on error
display dialog "The targeted process is not running"
end try
end getProcMB

getProcMB("Safari")
__________________
17" MBP, OS X; 27" iMac, both OS X 10.10.x (latest)

Last edited by NovaScotian; 12-14-2006 at 11:49 AM.
NovaScotian is offline   Reply With Quote
Old 12-14-2006, 11:55 AM   #4
bramley
MVP
 
Join Date: Apr 2004
Location: Cumbria, UK
Posts: 2,461
Quote:
Originally Posted by kainewynd2
A simple copy paste of the first code to Applescript doesn't seem to accomplish anything - no errors and no response.

Hayne should have added that when saving as app to check the "stay open" checkbox in the save dialog. Note you cannot run the script from the editor.
Quote:
Originally Posted by kainewynd2
I canceled the process after these many popped up. Is that by design?

The message means that you have got the 24UAppearance Scripting Addition on your machine and it there is a problem with loading it i.e. the message is not relevant to the script - it is a general problem with Applescript. The scripting addition is either missing or corrupt. If you need it I'd re-install it.

I've not had it crash on me - yet. Will run it for a while.
bramley is offline   Reply With Quote
Old 12-14-2006, 12:09 PM   #5
NovaScotian
League Commissioner
 
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 5,155
With respect to appIsRunning, I would have done this:

on isRunning(appName)
tell application "System Events" to if exists process appName then return true
return false
end isRunning

isRunning("Safari")
__________________
17" MBP, OS X; 27" iMac, both OS X 10.10.x (latest)
NovaScotian is offline   Reply With Quote
Old 12-14-2006, 12:36 PM   #6
NovaScotian
League Commissioner
 
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 5,155
My final version (which runs on my machine)

Code:
on showAppSizeInTitle(appName, warnMB)
	if my isRunning(appName) then
		set MB to my getProcMB(appName)
		tell application appName
			set dlm to " *** "
			set title to name of window 1
			set Orig to my removeLastPart(title, dlm)
			set name of window 1 to Orig & dlm & MB
			if MB > warnMB then display alert appName & " is taking more than " & warnMB & " MB"
		end tell
	end if
end showAppSizeInTitle

on isRunning(appName)
	tell application "System Events" to if exists process appName then return true
	return false
end isRunning

to getProcMB(procName) -- as string
	try
		return ((last word of (do shell script "/bin/ps -xc -o command,rss | grep " & procName)) as integer) / 1024 -- result of a division will be real
	end try
end getProcMB

on removeLastPart(str, delim)
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to delim
	if (count str's text items) > 1 then set str to str's text item 1
	set AppleScript's text item delimiters to tid
	return str
end removeLastPart

--on idle
showAppSizeInTitle("Safari", 50)
--return 60
-- end idle
__________________
17" MBP, OS X; 27" iMac, both OS X 10.10.x (latest)
NovaScotian is offline   Reply With Quote
Old 12-14-2006, 12:58 PM   #7
bramley
MVP
 
Join Date: Apr 2004
Location: Cumbria, UK
Posts: 2,461
And mine is ...
Code:
-- This AppleScript displays the amount of memory being used by Safari
-- in the title of the frontmost Safari window.
-- It is intended to be saved as an application
-- and left running all of the time.
-- Cameron Hayne (macdev@hayne.net)  December 2006

-- getProcessMB:
-- Returns the number of megabytes used by the specified process.
-- It gets this value by invoking the Unix 'ps' command via a Perl script.
on getProcessMB(procName)
	set perlCode to "
# This script gets the 'RSS' of the process specified as 'procName'
# The 'RSS' (resident set size) is a rough measure of how much RAM
# the process is using. The value is given in megabytes.
open(PS, \"/bin/ps -xc -o command,rss |\");
while (<PS>)
{
    if (/^\\s*" & procName & "\\s+(\\d+)\\s*$/o)
    {
        my $rss = $1;
        my $rssMB = sprintf(\"%.1f\", $rss / 1024);
        print $rssMB;
        last;
    }
}
close(PS);
"
	set numMB to do shell script "perl -e " & quoted form of perlCode
	return numMB as real
end getProcessMB

-- removeLastPart:
-- Returns the string 'str' without the last part starting with 'delim'
on removeLastPart(str, delim)
	set saveDelim to AppleScript's text item delimiters
	set AppleScript's text item delimiters to delim
	if (count str's text items) > 1 then
		set str to str's text 1 thru text item -2
	end if
	set AppleScript's text item delimiters to saveDelim
	return str
end removeLastPart

-- appIsRunning:
-- Returns true if the app 'appName' is running, otherwise it returns false
on appIsRunning(appName)
	tell application "System Events" to return (exists process appName)
end appIsRunning

-- showAppSizeInTitle:
-- Displays the amount of memory being used by the app 'appName' in the title
-- of the frontmost window of that app.
-- It pops up an alert dialog if the amount of memory used
-- is greater than 'warnMB' (megabytes).                
on showAppSizeInTitle(appName, warnMB)
	if my appIsRunning(appName) then
		tell application appName
			if (count of windows) > 0 then
				set title to name of window 1
				set delim to " *** "
				set origTitle to my removeLastPart(title, delim)
				set numMB to my getProcessMB(appName)
				set title to origTitle & delim & numMB & " MB"
				set name of window 1 to title
				
				if numMB is greater than warnMB then
					display alert appName & " is taking more than " & warnMB & " MB"
				end if
			end if
		end tell
	end if
end showAppSizeInTitle

on idle
	my showAppSizeInTitle("Safari", 150)
	return 10 -- updates every 10 seconds
end idle
I've used Hayne's original code cos it works here - and I'm still reading the man page for 'ps'!

Note script crashes if you accidentally close all windows - so I've added in a 'count the windows first' check above.

Interesting that when started here (without a homepage) Safari uses 48.6 MB, and takes up more memory thereafter. Resetting the cache drops this down. So Safari is storing at least some component of the cache in RAM - so is Apple doing this for performance reasons or because it's a bug?

One could have the script auto-quit the app and then re-launch it. Or clear the cache - there is a keyboard shortcut that could be called from Applescript - although this means clearing the whole cache, and may not be ideal.
bramley is offline   Reply With Quote
Old 12-14-2006, 01:39 PM   #8
NovaScotian
League Commissioner
 
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 5,155
You can avoid the crash by substituting this for my isRunning:

Code:
on isRunningHasWindow(appName)
	tell application "System Events" to if exists process appName then
		tell application appName
			try
				get name of window 1
				return true
			on error
				return false
			end try
		end tell
	end if
	return false
end isRunningHasWindow
__________________
17" MBP, OS X; 27" iMac, both OS X 10.10.x (latest)

Last edited by NovaScotian; 12-14-2006 at 02:05 PM. Reason: Didn't want to open Safari if not running
NovaScotian is offline   Reply With Quote
Old 12-14-2006, 02:48 PM   #9
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
Quote:
Originally Posted by NovaScotian
The first script fails on mm with the message: "Result of numeric operation was too large" - I think because of a divide by zero somewhere, possibly.

Hmm - I'd be very interested if you could track this down more.
I (obviously) don't see this in my tests. I don't see anywhere that I could be dividing by zero.

Quote:
As I go through the handlers, on mm the first: "getProcessMB" produces a null result.

It would do that if the process ("Safari") was not found in the results of the 'ps' command - e.g. if it was not running.

Maybe you could try the following stand-alone Perl script to see if you get similar problems.
Sample usage:
getProcessRss Safari

Code:
#!/usr/bin/perl

# getProcessRss:
# This script gets the 'RSS' of the process specified as a command-line arg.
# The 'RSS' (resident set size) is a rough measure of how much RAM
# the process is using.
# Example of use:  getProcessRss Safari
#
# Cameron Hayne (macdev@hayne.net)  December 2006

use warnings;
use strict;

die "Usage: getProcessRss: name_of_process\n" unless scalar(@ARGV) == 1;
my $procName = $ARGV[0];

my $psCmd = "/bin/ps -xc -o command,rss";

open(PS, "$psCmd |") or die "Can't run command \"$psCmd\": $!\n";
while (<PS>)
{
    if (/^\s*$procName\s+(\d+)\s*$/o)
    {
        my $rss = $1;
        my $rssMB = sprintf("%.1f", $rss / 1024);
        print "$rssMB MB\n";
        last;
    }
}
close(PS);
Quote:
The AppleScript below works for me in its place (I haven't examined the others yet):

to getProcMB(procName) -- as string
try
return ((last word of (do shell script "/bin/ps -xc -o command,rss | grep " & procName)) as integer) / 1024 -- result of a division will be real
on error
display dialog "The targeted process is not running"
end try
end getProcMB

getProcMB("Safari")

The problem with your version is that it will screw up if there happens to be more than one app running whose name includes the specified 'procName'.
There is usually only one app running whose name includes the string "Safari" so it will usually work in this case.
But it will fail if you tried it with "iTunes" since there is also an "iTunes Helper" app running.
__________________
hayne.net/macosx.html
hayne is offline   Reply With Quote
Old 12-14-2006, 02:53 PM   #10
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
Quote:
Originally Posted by bramley
Hayne should have added that when saving as app to check the "stay open" checkbox in the save dialog. Note you cannot run the script from the editor.

Thanks - I edited my post above to add these notes.
__________________
hayne.net/macosx.html
hayne is offline   Reply With Quote
Old 12-14-2006, 03:10 PM   #11
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
Here's a revised version of the "VersionA" script (the AppleScript application) that integrates the improvements of NovaScotian and bramley:
Code:
-- This AppleScript displays the amount of memory being used by Safari
-- in the title of the frontmost Safari window.
-- It is intended to be saved as an application
-- and left running all of the time.
-- Cameron Hayne (macdev@hayne.net)  December 2006

-- getProcessMB:
-- Returns the number of megabytes used by the specified process.
-- It gets this value by invoking the Unix 'ps' command via a Perl script.
on getProcessMB(procName)
    set perlCode to "
# This script gets the 'RSS' of the process specified as 'procName'
# The 'RSS' (resident set size) is a rough measure of how much RAM
# the process is using. The value is given in megabytes.
open(PS, \"/bin/ps -xc -o command,rss |\");
while (<PS>)
{
    if (/^\\s*" & procName & "\\s+(\\d+)\\s*$/o)
    {
        my $rss = $1;
        my $rssMB = sprintf(\"%.1f\", $rss / 1024);
        print $rssMB;
        last;
    }
}
close(PS);
"
    set numMB to do shell script "perl -e " & quoted form of perlCode
    return numMB as real
end getProcessMB

-- removeLastPart:
-- Returns the string 'str' without the last part starting with 'delim'
on removeLastPart(str, delim)
    set saveDelim to AppleScript's text item delimiters
    set AppleScript's text item delimiters to delim
    if (count str's text items) > 1 then
        set str to str's text 1 thru text item -2
    end if
    set AppleScript's text item delimiters to saveDelim
    return str
end removeLastPart

-- appIsRunning:
-- Returns true if the app 'appName' is running, otherwise returns false
on appIsRunning(appName)
    set isRunning to false
    tell application "System Events"
        if exists process appName then
            set isRunning to true
        end if
    end tell
    return isRunning
end appIsRunning

-- showAppSizeInTitle:
-- Displays the amount of memory being used by the app 'appName' in the title
-- of the frontmost window of that app.
-- It pops up an alert dialog if the amount of memory used
-- is greater than 'warnMB' (megabytes).                
on showAppSizeInTitle(appName, warnMB)
    if my appIsRunning(appName) then
        tell application appName
            set numMB to my getProcessMB(appName)
            if (count of windows) > 0 then
                set title to name of window 1
                set delim to " *** "
                set origTitle to my removeLastPart(title, delim)
                set title to origTitle & delim & numMB & " MB"
                set name of window 1 to title
            end if
            if numMB is greater than warnMB then
                display alert appName & " is taking more than " & warnMB & " MB"
            end if
        end tell
    end if
end showAppSizeInTitle

on idle
    showAppSizeInTitle("Safari", 150)
    return 10 -- updates every 10 seconds
end idle
I only put the title-changing inside the if-statement that checks for windows existing since I want it to pop up the alert even if there are no windows.
But I haven't tested that this alert works if there are no windows - I'm just assuming it does.

By the way, I kept the 'appIsRunning' subroutine in the more verbose form since it is easier to generalize it starting from that form. E.g. if I wanted later to add another condition on the app.

Another note: I kept the 'removeLastPart' subroutine using:
set str to str's text 1 thru text item -2
instead of the simpler:
set str to str's text 1
since that way it is more robust in the (hopefully unusual) case where the delimiter (" *** ") happens to occur in the original title.

I'm not sure why, but I now am finding that this AppleScript application is taking about 5 MB of RAM whereas before it was taking more than 10 MB. I'm not sure what caused this difference.

By the way, if I change the script to use the following in place of the 'on idle' handler, I find that it takes about twice as much CPU. It still is not a significant amount of CPU, but it seems to be averaging about 0.4 % CPU with the 'repeat' loop compared with about 0.2 % with the 'on idle'.

Code:
repeat
	showAppSizeInTitle("Safari", 150)
	delay 10 -- updates every 10 seconds
end repeat
(Note that with the 'repeat' loop instead of the 'on idle', it is possible to test the script in Script Editor)
__________________
hayne.net/macosx.html

Last edited by hayne; 12-15-2006 at 04:02 AM.
hayne is offline   Reply With Quote
Old 12-14-2006, 05:21 PM   #12
NovaScotian
League Commissioner
 
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 5,155
I've tried the new version, Hayne, (and the perl script itself) and neither of them return a result for me.

In my /usr/bin/, I find perl and perl5.8.6. Can that be confusing the issue? I've never run either of them before.
__________________
17" MBP, OS X; 27" iMac, both OS X 10.10.x (latest)

Last edited by NovaScotian; 12-14-2006 at 05:50 PM. Reason: Added Info
NovaScotian is offline   Reply With Quote
Old 12-14-2006, 05:50 PM   #13
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
Quote:
Originally Posted by NovaScotian
I've tried the new version, Hayne, (and the perl script itself) and neither of them return a result for me.

Hmm - very odd.
What model of Mac and what version of OS X do you have?

Could you please run the following command (in a Terminal window) and then show us the results (I'm assuming that you have Safari running):

/bin/ps -xc -o command,rss | grep Safari | vis -w
__________________
hayne.net/macosx.html
hayne is offline   Reply With Quote
Old 12-14-2006, 06:00 PM   #14
NovaScotian
League Commissioner
 
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 5,155
Quote:
Originally Posted by hayne
/bin/ps -xc -o command,rss | grep Safari | vis -w

Safari\040\040\040\040\040\040\040\040\040\040\040\04037000\^JACB-G5:~ bellac$

where there was clearly no return after the instruction since ACB-G5:~bellac is my normal prompt.

My machine/system are in the signature (you must turn them off): dual-core G5/2.3 running Mac OS X 10.4.8
__________________
17" MBP, OS X; 27" iMac, both OS X 10.10.x (latest)
NovaScotian is offline   Reply With Quote
Old 12-14-2006, 06:40 PM   #15
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
Quote:
Originally Posted by NovaScotian
Safari\040\040\040\040\040\040\040\040\040\040\040\04037000\^JACB-G5:~ bellac$

where there was clearly no return after the instruction since ACB-G5:~bellac is my normal prompt

The ^J is the return.

Could you please run the following modified version of the stand-alone Perl script and show us what you get from:
getProcessRss Safari

Code:
#!/usr/bin/perl

# getProcessRss:
# This script gets the 'RSS' of the process specified as a command-line arg.
# The 'RSS' (resident set size) is a rough measure of how much RAM
# the process is using.
# Example of use:  getProcessRss Safari
#
# Cameron Hayne (macdev@hayne.net)  December 2006

use warnings;
use strict;

die "Usage: getProcessRss: name_of_process\n" unless scalar(@ARGV) == 1;
my $procName = $ARGV[0];
print "Looking for: \"$procName\"\n";

my $psCmd = "/bin/ps -xc -o command,rss";

open(PS, "$psCmd |") or die "Can't run command \"$psCmd\": $!\n";
while (<PS>)
{
    if (/$procName/o)
    {
        print "PS: \"$_\"\n";
    }

    if (/^\s*$procName\s+(\d+)\s*$/o)
    {
        my $rss = $1; 
        my $rssMB = sprintf("%.1f", $rss / 1024);
        print "$rssMB MB\n";
        last;
    }
}
close(PS);
__________________
hayne.net/macosx.html
hayne is offline   Reply With Quote
Old 12-14-2006, 08:58 PM   #16
NovaScotian
League Commissioner
 
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 5,155
I'm the wrong guy to do this Hayne - I'm convinced now that my problem is that I'm doing something wrong in Terminal to get your file running. If you want me to run it you'll have to supply the command line to do it. I've become totally confused and don't even know whether I've rendered your perl script executable properly, and certainly don't know how to run a perl script. perl is usually silent on all the variations I've tried.

Sorry
__________________
17" MBP, OS X; 27" iMac, both OS X 10.10.x (latest)
NovaScotian is offline   Reply With Quote
Old 12-14-2006, 10:28 PM   #17
trevor
Moderator
 
Join Date: Jun 2003
Location: Boulder, CO USA
Posts: 19,853
NovaScotian:

1. In your browser, select the perl script above, hit command-C to copy it to the clipboard.

2. In your Terminal, type
sudo pico /usr/local/bin/getProcessRss
(this assumes that you have a /usr/local/bin directory. If you don't, you should--create one with sudo mkdir /usr/local/bin and add it to your PATH in your shell configuration file, such as ~/.bash_profile or ~/.bashrc)

3. A simple text editor, pico, will open in your Terminal. Now, hit command-V to paste the script in your clipboard into pico. Now, hit control-X to save, and hit the Y key to confirm, then hit Return. (Note that you are using the Mac's clipboard when pasting, so you use the command key. When you are in pico, you are using a Unix app, so you use the control key.

4. Enter the following command to make the script executable:
sudo chmod 555 /usr/local/bin/getProcessRss

5. Test it as hayne requests.

Trevor
trevor is offline   Reply With Quote
Old 12-14-2006, 10:35 PM   #18
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
Quote:
Originally Posted by NovaScotian
I'm the wrong guy to do this Hayne - I'm convinced now that my problem is that I'm doing something wrong in Terminal to get your file running. If you want me to run it you'll have to supply the command line to do it. I've become totally confused and don't even know whether I've rendered your perl script executable properly, and certainly don't know how to run a perl script. perl is usually silent on all the variations I've tried.

Okay, you've saved it in a file, right?
Open that file in TextWrangler (or some other text editor that can tell you about the line endings) and click (once) on the toolbar icon at the top that looks like a little page. This will bring down a menu where the first 3 menu items are: Macintosh, Unix, DOS. You want it to be Unix. Save the file if you had to change this.

Now, go into a Terminal window and navigate to the folder where you saved that script. (See the "navigation" section of this Unix FAQ if you need help with this)
Then type in (or copy & paste from here) the commands:
chmod +x getProcessRss
./getProcessRss Safari
where I have assumed that you saved the script in a file named "getProcessRss".
The first of these commands ('chmod') makes the script executable. The second command runs the script with a command-line argument of "Safari".
__________________
hayne.net/macosx.html

Last edited by hayne; 12-14-2006 at 10:38 PM.
hayne is offline   Reply With Quote
Old 12-15-2006, 12:35 AM   #19
NovaScotian
League Commissioner
 
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 5,155
Thank you both, gentlemen. The first step on Hayne's agenda was an unknown to me. I use BBEdit, but had never noticed that I had a choice of line endings - in AppleScript that's looked after for you.

I altered your processes slightly, but the result was:
ACB-G5:~/Desktop/Hayne bellac$ ./getProcessRss Safari
Looking for: "Safari"
PS: "Safari 42460
"
41.5 MB

I found it easier (because editing typing in the Terminal is so unnatural for me) to type chmod +x and drag the file icon to the terminal and return (I use iTerm).

Then cd and drag the folder it's in to the Terminal, return again

Then ./getProcessRss Safari -- after opening Safari, but it works equally well for Camino.
__________________
17" MBP, OS X; 27" iMac, both OS X 10.10.x (latest)
NovaScotian is offline   Reply With Quote
Old 12-15-2006, 01:33 AM   #20
mark hunte
MVP
 
Join Date: Apr 2004
Location: Hello London Calling
Posts: 1,787
Just got to This.

The Latest script runs fine, with Safari running or not.
My Safari seems to be running at around 25.X MB.
But the VersionA runs at 14.x MB.
mark hunte is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 03:45 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.