Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Running ps -ax -O 'rss user %mem should summon all the processes that are currently contributing to either Active or Wired , in the Mac definitions of

"Active + Wired + Inactive + Free = Total Physical Memory" , right?

Activity Monitor was showing high Active mem usage and hand-counting showed low numbers, so I re-counted with the below awk script.

Note the Activity Monitor screenshot 1 for 'Active' and 'Wired'. At the time of capture, 'root' processes show ~377MiB and the not-root total ~1805MiB is close.

total: root  376632 KiB, and  active  1805476 KiB , percent :  51.9
% root  8.4
% active  43.5

screenshot

However, closing Chrome makes the problem clearer. I have an Activity Monitor screenshot 2 , with script output. Here, neither Active nor Wired memory matches.

total: root  365968 KiB, and  active  643880 KiB , percent :  23.6
% root  8.2
% active  15.4

screenshot

mem_count.sh:

 3 ps -axmc -O 'rss user %mem' | awk '
 4 
 5 BEGIN { 
 6     root = 0 ; active = 0 ; percent_root = 0.0 ;
 7     percent_active = 0.0  ; percent = 0.0 ; 
 8 } 
 9 
10 { 
11 percent += $4
12 if ( $3 == "root")  {  
13     root += $2 
14     percent_root  += $4 ; 
15 } 
16 else {  
17     active += $2  
18     percent_active  += $4 ; 
19 } 
20 
21 } 
22 END {
23     print "total: root ", root, "KiB, and  active ", active , "KiB , percent : ", percent  ; 
24     print "% root " , percent_root      
25     print "% active " , percent_active    
26 }'

sample output

$ ps -axmc -O 'rss user %mem'
PID    RSS USER             %MEM   TT  STAT      TIME COMMAND
37 160964 root              3.8   ??  Ss    20:22.92 mds 
...

This is causing a bit of a headache. Image Ref:

share|improve this question
    
You might want to check out top. It's different from its Linux counterpart and does support programmatic output parsing like e.g. here. –  Daniel Beck Aug 26 '12 at 23:17
add comment

1 Answer

ps displays stats for user processes only. It does not report stats about the kernel. So, don't expect the memory reported by ps to ever add up to the total physical memory.

You also seem to have misunderstood some key concepts of unix-like operating systems and OSX memory management. I can't tell which exactly, but here

  1. root != kernel: root is a user as well.
  2. wired memory != memory used by root processes
  3. active memory != memory used by your processes
  4. inactive memory does not work the way one may think: It also includes memory from running processes. Read this question which provides some insight on inactive memory.

The bottom line is that there is no trivial correlation between the aggregate memory statistics of the Activity Monitor and the memory usage statistics reported by ps.

share|improve this answer
    
That's true except when -a is used with -x . -A and -ax are supposed to be equivalent forms. But I'll do a sanity check! –  HeyWatchThis Aug 24 '12 at 19:51
    
Yea no difference. Thanks though. –  HeyWatchThis Aug 24 '12 at 19:53
    
@HeyWatchThis There's no line with PID 0 in your ps output. That'd be the kernel (compare Activity Monitor output). –  Daniel Beck Aug 26 '12 at 23:15
    
@DanielBeck Exactly my point. ps does not report stats for the kernel (showing up as with pid 0 in Activity Monitor and other tools - e.g. top). Even if you explicitely ask for this process (ps -p 0), ps will not report anything. –  m000 Aug 27 '12 at 9:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.