Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Memory usage on Mac

by aixtal (Sexton)
on May 24, 2013 at 12:08 UTC ( [id://1035120]=perlquestion: print w/replies, xml ) Need Help??

aixtal has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I am aware of Memory::Usage, but it works only under Linux (using /proc). Is there any equivalent for Mac OS ? Or better yet a single module or wrapper that would handle both platforms ?

I have the feeling that this should have been asked in the past, but could not find it in Super search.

Many thanks

--jean

Replies are listed 'Best First'.
Re: Memory usage on Mac (mems tasklistMem memstatMem procMem pslistMem)
by Anonymous Monk on May 24, 2013 at 18:44 UTC

    This *might* help, its a start, needs refinement

    #!/usr/bin/perl -- use strict; use warnings; Main( @ARGV ); exit( 0 ); sub Main { @_ = $$ if @_ and $_[0] eq 'self' ; @_ or die "\nUsage: $0 pid\nUsage: $0 self\n"; print mems(@_),"\n"; } use constant GOTPROC => !!-f "/proc/$$/statm"; use File::Which qw/ which /; use vars qw' $GOTPS $GOTPSLIST $GOTTASKLIST $GOTMEMSTAT '; BEGIN { $GOTPS = which('ps'); $GOTPSLIST = which('pslist.exe'); $GOTTASKLIST = which('tasklist.exe'); $GOTMEMSTAT = which('memstat.exe'); } sub mems { die "pid must be a number, not ((($_[0])))" unless $_[0] =~ /^\-?\ +d+\Z/; GOTPROC and goto &procMem; $GOTPSLIST and goto &pslistMem; $GOTTASKLIST and goto &tasklistMem; #~ $GOTMEMSTAT and goto &memstatMem; # unfinished $GOTPS and goto &psMem; die "NO SUITABLE MEMORY FUNCTION FOUND"; } sub memstatMem { my( $pid ) = @_; my $mem = qx{$GOTMEMSTAT -p $pid}; chomp $mem; return $mem; } sub tasklistMem { my( $pid ) = @_; #~ /nh no headers my @lines = qx[$GOTTASKLIST /nh /fi "pid eq $pid"]; chomp @lines; #~ Image Name PID Session Name Session# Mem U +sage #~ ========================= ====== ================ ======== ======== +==== #~ perl.exe 888 0 101,0 +08 K my %ps; @ps{'ImageName', 'PID', 'SessionName', 'Session#', 'Mem' } = grep length, split /\s+/, $lines[-1]; return "Mem: $ps{Mem}"; } sub procMem { my( $pid ) = @_; my $mem = `cat /proc/$pid/statm`; $mem =~ s/^(\d+).*/$1/s; return "proc{ $mem }"; } sub psMem { my( $pid ) = @_; my @lines = qx{$GOTPS -o vsize,rss -p $pid }; chomp @lines; foreach my $line (@lines) { my ( $vsize, $rss) = split /\s+/, $line; return "VM: $vsize RSS: $rss "; } return; } sub pslistMem { my( $pid ) = @_; my @lines = qx{$GOTPSLIST -m $pid 2>NUL}; chomp @lines; #~ Name Pid VM WS Priv Priv Pk Faults +NonP Page #~ perl 1052 25348 7220 4504 4512 1977 + 2 33 my %ps; @ps{'Name', 'Pid', 'VM', 'WS', 'Priv', 'Priv Pk', 'Faults', 'NonP', 'Page' } = grep length, split /\s+/, $lines[-1]; #~ Pri Priority #~ Thd Number of Threads #~ Hnd Number of Handles #~ VM Virtual Memory #~ WS Working Set ------ "Mem Usage" in taskmanager #~ Priv Private Virtual Memory ------ "VM Size" in taskman +ager #~ Priv Pk Private Virtual Memory Peak #~ Faults Page Faults #~ NonP Non-Paged Pool #~ Page Paged Pool #~ Cswtch Context Switches #~ use DDS; Dump(\@lines , \%ps); return "WVM: $ps{VM} { WS: $ps{WS} VM: $ps{Priv} } "; } #~ https://en.wikipedia.org/wiki/Resident_Set_Size #~ https://en.wikipedia.org/wiki/Working_set_size #~ https://en.wikipedia.org/wiki/Virtual_memory

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1035120]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-03-19 05:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found