Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Display memory usage ala OS 9 System
From an contributor who wishes to remain anonymous, here's a script that brings back an OS 9 style memory usage report. Here's what he wrote:

here is a script to give a full memory report. i wanted something to give me a report as useful and easy to read as the About This Mac box in OS 9. the script provides a simple rundown of:
  • number of swapfiles
  • number of pageouts
  • free memory
  • physical memory used by top 7 apps
Notes on the script:
  • save it as a application, or save it as a compiled script and run it from the Script Menu.
  • you can vary the number of applications reported by changing the number in the first line of the script.
  • the script tries some kludgey workarounds to get application names from 'ps aux'. it assumes native non-system apps will reside in the Applications folder. your mileage may vary with regards to app names, but it should be better than those 'LaunchCFMApp' names in ProcessViewer. if anyone knows a better way to get app names, please post.
Read the rest of the article for the script...

NOTE: This script has been substantially revised! It should now parse almost any possible application scheme correctly!

NOTE #2: Revised again!

set num to 9

global MRpid, MRrss, MRcom
do shell script "ls -l /var/vm/"
set c to number of paragraphs of result
set c to c - 1 as string
do shell script "vm_stat"
set r to result
set rt to return

set t to vmparse(r, 10) as string
set q to pageouts(t)
set dd to rt & "Swapfiles: " & c & rt
set dd to dd & "Pageouts: " & q
set dd to dd & rt & rt & rt

set a to vmparse(r, 1)
set t to vmmeg(a)
set dd to dd & "Free: " & t & " MB "
set dd to dd & rt & rt & rt

do shell script "ps aux -m -w -w -c"
set tres to result
set r to paragraphs 2 through (num + 2) of tres as list
set h to paragraph 1 of tres as string
set t to offset of "PID" in h
set MRpid to [(t - 2), (t + 2)]
set t to offset of "RSS" in h
set MRrss to [(t - 3), (t + 3)]
set t to offset of "COMMAND" in h
set MRcom to (t + 1)

repeat with c from 1 to num
set a to item c of r
set dd to dd & psmem(a) & " "
set dd to dd & psname(a) & rt
end repeat

display dialog dd & rt

on pageouts(a)
set a to a * 4
if a > 1024 then
set k to " MB"
set a to a / 1024
set a to a as real
else if a = 0 then
set k to ""
else
set k to " KB"
end if
set a to intnum(a)
return a & k
end pageouts

on vmparse(x, n)
set AppleScript's text item delimiters to {"."}
set y to text item n of x as string
set AppleScript's text item delimiters to {""}
set z to text items -1 through -8 of y as string
set a to lspace(z)
set a to a as integer
return a
end vmparse

on vmmeg(m)
set z to m / 256
set z to z as string
set a to intnum(z)
return a
end vmmeg

on psmem(r)
set d1 to item 1 of MRrss
set d2 to item 2 of MRrss
set AppleScript's text item delimiters to {""}
set t to text items d1 through d2 of r as string
set m to lspace(t)
set z to m / 1024
set z to intnum(z)
if z as number < 10 then set z to " " & z as string
return (z & " MB")
end psmem

on psname(r)
set AppleScript's text item delimiters to {""}
set pname to text items MRcom through -1 of r as string
if pname = "LaunchCFMApp" then
set d1 to item 1 of MRpid
set d2 to item 2 of MRpid
set pid to text items d1 through d2 of r as string
do shell script "ps " & pid & " -w -w"
set tres to result
set fo to offset of "COMMAND" in (paragraph 1 of tres)
set t to paragraph 2 of tres
set t to text items fo through -1 of t as string
set AppleScript's text item delimiters to {"/"}
set t to text item -1 of t as string
set tr to lspace(t)
set os to offset of tr in t
set pname to text items 1 through (os - 2) of t as string
end if
return pname
end psname

on lspace(t)
set AppleScript's text item delimiters to {" "}
set m to text item -1 of t
set AppleScript's text item delimiters to {""}
return m
end lspace

on intnum(x)
set x to x as string
set AppleScript's text item delimiters to {"."}
set z to text item 1 of x
set AppleScript's text item delimiters to {""}
return z
end intnum
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[6,154 views]  

Display memory usage ala OS 9 | 31 comments | Create New Account
Click here to return to the 'Display memory usage ala OS 9' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
AppleScript Studio Anyone?
Authored by: petey on Feb 05, '02 10:23:58AM

Anyone proficient with AS Studio want build this as an app?

That way you could throw the script in an idle handler, so it could display its info in a non-modal window that would update every 5 seconds or so.

If you wanted to get ambitious, you could add buttons to quit an app and quit/relaunch an app. (quit/relaunch seems to be a good way to free up memory.)

I'm going to have to learn AS Studio one of these days.



[ Reply to This | # ]
AppleScript Studio Anyone?
Authored by: porkchop_d_clown on Feb 05, '02 11:29:36AM

Couldn't you just use the process viewer or top for that?



[ Reply to This | # ]
I can't compile this
Authored by: porkchop_d_clown on Feb 05, '02 11:14:21AM

I get an error the error 'Expected "," or "}" but found plural class name instead.

The offending line is:

if text item 2 of n = "Applications" then

I think the problem is in the previous line where it says "". should that be "/"?



[ Reply to This | # ]
I can't compile this
Authored by: porkchop_d_clown on Feb 05, '02 11:27:18AM

Answered my own question - change that backslash to a forward slash to compile.



[ Reply to This | # ]
Just fixed the source...
Authored by: robg on Feb 05, '02 11:31:55AM

Sorry for the troubles; I just fixed the source, too. Copy/paste should now work properly.

-rob.



[ Reply to This | # ]
Subfolders in Applications
Authored by: ElLeon0 on Feb 05, '02 01:37:24PM
For those of you who have all your programs stored within subfolders of Applications (e.g. Applications/Internet or Applications/Multimedia), make the following change in the psname(a) function to get the application names to display properly:

if text item 2 of n = "Applications" then

set nn to text item 4 of n


(The 3 is changed to a 4.)

Now the script works great for me.

[ Reply to This | # ]
Great Tip!
Authored by: thatch on Feb 05, '02 05:59:07PM

Worked for me without any edit, first time.

I never knew I would have 901 pageouts with only 7 processes running on my G4 867 with 896 Ram. Yikes! But I still have 578 MB free so I guess all is well. Everything is working great. Thank you!



[ Reply to This | # ]
doesn't like fink.
Authored by: charlietuna on Feb 05, '02 03:28:16PM

This script complains that it:

Can't get text item 6 of "perl -w /sw/bin/fink update-all".

Does it fail with others' shell utilities or Xfree86?



[ Reply to This | # ]
doesn't like fink.
Authored by: petey on Feb 05, '02 06:44:13PM

one solution for better name parsing in situations like this:

the script could get 'ps aux' twice, once the way it currently does with app paths, and then a second time as 'ps aux -m -w -w -c', which returns the executable's name. then it could check if the executable's name is 'LaunchCFMApp' or not, and only call the kludgey name parsing if it is.



[ Reply to This | # ]
Fixed Script Coming...
Authored by: petey on Feb 05, '02 09:39:13PM

I figured out how to accurately get Application names without all the klugdes. And also managed to make the clean up thescript and make it run quicker in the process.

I sent the updated script to Rob to repost, so it should be coming soon.



[ Reply to This | # ]
It's there now!
Authored by: robg on Feb 06, '02 01:26:27AM

After some hard work by petey, the revised script is now online. I just gave it the old "cut and paste" test, and it worked fine here, so it should be working for others as well.

Very cool little AppleScript, I must say!

-rob.



[ Reply to This | # ]
Works even better!
Authored by: thatch on Feb 06, '02 03:20:15AM

It shows the names for the processes more accurately now. Great work and thank you!



[ Reply to This | # ]
My version...
Authored by: porkchop_d_clown on Feb 06, '02 11:48:40AM

First, I want to thank the author of the original script, it intrigued me enough to get off my butt and learn some applescript (I'm a UNIX programmer by day).

With that said, I think you will find this version shorter and considerably faster than the original - first, because it doesn't need to make repeated calls to the shell, second because it uses math rather than string manipulation to round the results.

Please tell me what you think!



set MaxProcs to 5 set dstring to "OS X Virtual Memory Statistics:" & return set dstring to dstring & get_swap_info() set dstring to dstring & return & return & "Top " & MaxProcs & " Applications: " set dstring to dstring & get_big_procs(MaxProcs) display dialog dstring buttons {"Ok"} default button 1 -- Parses out the value of one line of output from vm_stat and -- returns it as a string in MB. on vmparse(x, n) -- extract the nth line of the output. set AppleScript's text item delimiters to {"."} set y to text item n of x as string -- Convert the # 4k pages to the nearest megabyte set AppleScript's text item delimiters to {" "} set a to (word -1 of y as integer) + 128 set a to (a - (a mod 256)) / 256 as integer return (a as string) & " MB" end vmparse -- Find out swap space stats and returns them as a string. on get_swap_info() -- get info about vm files do shell script "ls -l /var/vm" set r to result set c to number of paragraphs of r -- add up swap file sizes set t to 0 repeat with i from 2 to c set t to t + (word 6 of paragraph i of r) as integer end repeat -- round to the nearest megabyte. set t to t + 524288 set t to (t - (t mod 1048576)) / 1048576 as integer set o to " Total Swap: " & t & " MB in " & c - 1 & " files" do shell script "vm_stat" set r to result set o to o & return & " Used Swap: " & vmparse(r, 10) set o to o & return & " Free RAM : " & vmparse(r, 1) as string return o end get_swap_info


[ Reply to This | # ]
rest of the script
Authored by: porkchop_d_clown on Feb 06, '02 11:51:39AM

The comment form truncated the script. Here's the last part:

-- Returns a list of the largest num applications on get_big_procs(num) do shell script "ps auxmww" set r to paragraphs 2 through (num + 2) of result as list set o to "" repeat with c from 1 to num set e to item c of r -- get size rounded to nearest MB set vsz to ((word 6 of e) as integer) + 512 set vsz to ((vsz - (vsz mod 1024)) / 1024) as integer set o to o & return & " " & vsz & " MB " -- extract the process name set AppleScript's text item delimiters to {""} set ao to offset of "/" in e set a to (text items ao through -1 of e) as string -- if the name contains ".app", truncate the name there. set ao to offset of ".app" in a if (ao is not 0) then set a to (text items 1 through (ao - 1)) of a as string -- extract the executable name from the process name. set AppleScript's text item delimiters to {"/"} set a to text item -1 of a set o to o & a end repeat return o end get_big_procs


[ Reply to This | # ]
Max No of App Details?
Authored by: wuf810 on Feb 06, '02 07:12:23PM

Is there anyway to increase the number of Top "n" apps? Even though I modified the variable used for the loop, it appears (for me at least) that the maximum is 7. Any ideas?



[ Reply to This | # ]
Max No of App Details?
Authored by: porkchop_d_clown on Feb 06, '02 10:44:42PM

I think the problem is that there is some upper limit to the # of characters you can send to a dialog. Try this: replace the top portion of the script (everything above 'on vmparse(x,n)') with this code:


-- Change this to change the # of applications displayed.
set MaxProcs to 10

if (MaxProcs is less than or equal to 5) then
set dstring to "OS X Virtual Memory Statistics:" & return
set dstring to dstring & get_swap_info()
set dstring to dstring & return & return & "Top " & MaxProcs & " Applications: "
set dstring to dstring & get_big_procs(MaxProcs)
display dialog dstring buttons {"Ok"} default button 1
else
display dialog "OS X Virtual Memory Statistics:" & return & get_swap_info() buttons {"More Info", "Done"} default button 1

if (button returned of the result is "More Info") then
display dialog "Top " & MaxProcs & " Applications: " & get_big_procs(MaxProcs) buttons {"Done"} default button 1
end if
end if

This fragment will display one dialog if you ask for 5 or fewer apps, and two dialogs if you want more than 5. I'm also working on a version that handles UNIX command line arguments correctly.



[ Reply to This | # ]
Max No of App Details?
Authored by: petey on Feb 07, '02 02:55:34AM

rather than fragment into 2 dialogs, get rid of the text section headers. that gives room for a few more apps. using my layout, i can fit 9 - 11 depending on length of the app names.

and Porkchop,

i like your code much better than mine, but i like my display layout much better than yours. keep it sparse and easy to read at a glance. and keep apps under 10MB left aligned.

now that you've learned AS, will you please learn AS Studio? all i want for Christmas is to have this info always sitting in a window on the desktop. i'm limping along with 384MB, which really isn't enough for this OS, so i need to do manual memory management. actually, all i want for Christmas is for 512MB laptop RAM modules to get cheap.



[ Reply to This | # ]
Max No of App Details?
Authored by: porkchop_d_clown on Feb 08, '02 10:53:35AM

heh. No sympathy from me - I'm using a 256mb iBook.

Believe it or not, I was downloading the developer tools when I read your message last night. Stayed up till 3 playing with it. (No, I have no life whatsoever. :-P) I've got something that's so-so about doing what you want. I couldn't get it to refresh at a fixed interval, AppleScript has an "on idle" event but doesn't seem to have an "on timeout" or "on delay" event.

So, the result is that (on my box) it refreshes at odd intervals, every 20-60 seconds or so.

OSXHints doesn't really have a place to submit binaries, so drop me a note to my username at mac.com and I'll reply with an executable for you to play with.



[ Reply to This | # ]
My version...
Authored by: petey on Feb 06, '02 07:01:00PM

much more elegant code. using word & paragraph - who woulda thunk it?

it has some trouble on my machine getting some app names - (it appends the -psn_0_ stuff to the ends of some names)



[ Reply to This | # ]
-psn
Authored by: porkchop_d_clown on Feb 06, '02 10:34:51PM

Yeah - basically those are UNIX command line arguments. I'm not sure how to deal with them because there's no easy way to tell where the application name stops and the arguments begin, when the application name is allowed to have spaces in it. I'm pondering it though; we'll see what I can come up with.



[ Reply to This | # ]
-psn
Authored by: petey on Feb 06, '02 11:33:45PM

just filter out " -psn" the same way you filter out ".app"



[ Reply to This | # ]
-psn
Authored by: mcroft on Feb 06, '02 11:58:37PM

yup, I added this just after the filer for .app

-- if the name contains "-psn", truncate the name there.
set ao to offset of " -psn" in a
if (ao is not 0) then set a to (text items 1 through (ao - 1)) of a as string



[ Reply to This | # ]
-psn
Authored by: porkchop_d_clown on Feb 07, '02 05:25:19PM

Well, I want a general version that will kill any command line args. They almost always have a dash in front, and a space, so I think I have a solution.



[ Reply to This | # ]
What up w Pageout Number?
Authored by: petey on Feb 07, '02 02:21:39AM

I expected that the Pageout count would grow up to 80MB before the system built another Swapfile, but that hasn't been my experience so far. I seem to get new Swapfiles at 20MB increments of the Pageout count.

Any idea why this would be happening?

I'm almost 100% positive that Swapfiles are fixed at 80MB, so that means one of two things. Either:

- My Pageout calculation is wrong. I've been assuming each pageout as reported in vm_stat is 4K. Are they really 16K?

- Or, my understanding of the relationship betweeen Pageouts and Swapfiles is completely wrong.



[ Reply to This | # ]
What up w Pageout Number?
Authored by: porkchop_d_clown on Feb 08, '02 10:40:59AM

I don't think your calculations are wrong, I just think OS X has an aggressive VM system. Adding more swap is an expensive proposition, so the authors probably decided to add the second file way ahead of time, instead of risking a crash by waiting till your really about to run out.



[ Reply to This | # ]
Clean Code
Authored by: petey on Feb 07, '02 04:42:24AM
Inspired by Porkchop, I decided to see how clean I could get the code. (Annoyingly, the site strips out multiple spaces, so you'll have to add them back to the 3rd paragraph get the formatting lined up again.)
set num to 10 set r to return do shell script "ls -l /var/vm/" set swp to ((number of paragraphs of result) - 1) do shell script "vm_stat" set vSS to result do shell script "ps auxmww" set appSS to paragraphs 2 through (num + 1) of result as list set d to r & "Swapfiles: " & swp & r set d to d & "Pageouts: " & vParse(vSS, 11) & r & r & r set d to d & "Free: " & vParse(vSS, 2) & r & r & r repeat with c in appSS set d to d & appParse(c) & r end repeat display dialog d & r on vParse(x, xn) set v to word -1 of (paragraph xn of x) set v to (v - (v mod 256)) / 256 as integer if v > 0 then set v to v & " MB" return v end vParse on appParse(x) set m to word 6 of x set m to (m - (m mod 1024)) / 1024 as integer if 10 > m then set m to " " & " " & m set AppleScript's text item delimiters to {"/"} set n to text item -1 of x set AppleScript's text item delimiters to {""} set o to offset of " -psn" in n if (o > 0) then set n to (text items 1 through (o - 1)) of n as string if n = "TruBlueEnvironment " then set n to "Classic" return m & " MB " & " " & n end appParse

[ Reply to This | # ]
AppleScript Studio
Authored by: MadMax on Feb 21, '02 05:33:57AM
I've revised the code and built in AppleScript Studio.

The old "display dialog" of plain AS limit the output to 9-10 items. Now all tasks are listed in a scrollable field.

I've also modified some code to better display task list.

download the compiled app here
download the source here

[ Reply to This | # ]

AppleScript Studio
Authored by: petey on Feb 21, '02 07:51:22AM

MadMax,

You rock.

I wonder if there's any way to have the window automatically refresh every x seconds?

Now with your template, I should install the damn dev tools so I can change it to be just the way I want.



[ Reply to This | # ]
AppleScript Studio
Authored by: porkchop_d_clown on Feb 21, '02 10:28:36AM

Didn't I send you a version that did this back on the 7th? (Certainly people have been asking me for it)



[ Reply to This | # ]
Memory Usage Suggestions
Authored by: petey on Feb 21, '02 08:10:59AM

OK, well as long as I'm here, why don't i nitpick about the app? Here's what I'd want.

- User settable refresh rate. default maybe 20 sec?
- Get rid of the decimal points / keep the display cleaner.
- Use an antialiased font for the list.
- If it would reduce the memory or cpu footprint, limit the number of apps shown. I'm most concerned about the 6 - 10 biggest memory hogs anyway. Let the User set the number of apps to be displayed in the List.
- Allow the Window to be reduced further in size vertically. Currently, 14 apps must be shown at minimum, which is more than is necessary for a casual monitor.
- The smaller the window can be made the better, both horizontally and vertically, assuming folks might want to have it always going on their desktop without it being too imposing.



[ Reply to This | # ]
Error in Memory Usage App
Authored by: jonahlee on Jul 01, '02 04:16:48PM

I am running 10.1.5 and am getting an Applescript Error "Can't make ",05" into a number. (-1700) any clue what that means? I had the original applescript running as well, and am getting a similiar error.



[ Reply to This | # ]