Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Determining the Memory Usage of a Perl program from within Perl

by princepawn (Parson)
on Feb 15, 2001 at 07:23 UTC ( #58560=perlquestion: print w/ replies, xml ) Need Help??
princepawn has asked for the wisdom of the Perl Monks concerning the following question:

After having looked at Solaris::Procfs and BSD::Resource, I still am not sure whether there is some pre-written functionality that I can use to determine the memory usage of a running Perl program from within that program without issuing system calls.

Does anyone know of any such functionality?

Comment on Determining the Memory Usage of a Perl program from within Perl
Re: Determining the Memory Usage of a Perl program from within Perl
by Falkkin (Chaplain) on Feb 15, 2001 at 07:43 UTC
    I think that Devel::Peek's mstats function will do it for you. The documentation on CPAN doesn't give much help, but there's a place to start.
Re: Determining the Memory Usage of a Perl program from within Perl
by Gloom (Monk) on Feb 15, 2001 at 16:31 UTC
    Try
    open( STAT , "</proc/$$/stat" ) or die "Unable to open stat file"; @stat = split /\s+/ , <STAT>; close( STAT );

    You can find fields's description in man 5 proc

    _______________
    Hope this helps
      The Linux /proc fs is pretty nice because all of the data there (or what I found in there) is ascii readable. However, anyone know how to parse out the binary data in the Solaris /proc fs? I have never ever ever done any kind of parsing of data (non-ascii) in my life and while I know the basic I idea behind it I have no idea how to do it in Perl (or any other language for that matter :). Perhaps a few pointers? I have a feeling that it can be done with the '&' bitwise operator (I saw a script that did it that way one time) but I believe you need the offset for that to work?? I dunno. Any help on this subject would be quite appreciated.

      ----------
      - Jim

(tye)Re: Determining the Memory Usage of a Perl program from within Perl
by tye (Cardinal) on Feb 15, 2001 at 21:09 UTC

    First, note that there are at least two important measures of memory use: total memory size and working set size. I tend to oversimplify them slightly and think of them as "paging file use" and "RAM use", respectively. Total memory size is how many pages of virtual memory your program has been allocated. Working set size is how many pages of memory your program needs to have in RAM at once in order to run smoothly.

    If your program uses more total memory than is available to it (either due to quota restrictions or just how much is available to the operating system minus how much the kernel and other processes are using), it will probably die [well, usually malloc() will fail, which will usually either cause a quick death or trigger bugs which cause a slightly slower but more colorful death; but some operating systems will let malloc() succeed but when you actually try to use the memory the OS will notice the problem and kill you (or some other process!)].

    If your program needs a larger working set than is available, then it will start "paging" too much and slow down noticeably. The size of the working set has a lot to with how the memory is accessed. By making your data structures such that related items are close together in memory, you can often dramatically reduce your program's working set size. BTW, Perl plays this same trick by trying to arrange for its most-used C subroutines to be close together in memory (which is why we have the pp_hot.c file).

    To get the working set size, you'll have to ask the operating system. To get the total memory size, I'd build a version of Perl with DEBUGGING_MSTATS defined and then use Devel::Peek::mstats (though the documentation for this has been moved or purged so you may have to dig for it or reverse engineer it).

            - tye (but my friends call me "Tye")
Re: Determining the Memory Usage of a Perl program from within Perl
by Anonymous Monk on Feb 15, 2001 at 23:02 UTC
    Looking at the linux example, here is a short snippet for getting shared, resident, and size on systems with /proc.
    sub get_statm_info { $error = ''; my $ref = {}; if( ! open(_INFO,"</proc/$_[0]/statm") ){ $error = "Couldn't open /proc/$_[0]/statm [$!]"; return $ref; } my @info = split(/\s+/,<_INFO>); close(_INFO); ### these are all the props (skip some) # size resident shared trs lrs drs dt ### ### get the important ones $ref = {size => $info[0] * 4, resident => $info[1] * 4, shared => $info[2] * 4}; return $ref; }
    The values are in kb. If you don't care about shared, than the stat file is better:
    sub get_stat_info { $error = ''; my $ref = {}; ### open and read the main stat file if( ! open(_INFO,"</proc/$_[0]/stat") ){ $error = "Couldn't open /proc/$_[0]/stat [$!]"; return $ref; } my @info = split(/\s+/,<_INFO>); close(_INFO); ### these are all the props (skip some) # pid(0) comm(1) state ppid pgrp session tty # tpgid(7) flags minflt cminflt majflt cmajflt # utime(13) stime cutime cstime counter # priority(18) timeout itrealvalue starttime vsize rss # rlim(24) startcode endcode startstack kstkesp kstkeip # signal(30) blocked sigignore sigcatch wchan ### ### get the important ones $ref = {utime => $info[13] / 100, stime => $info[14] / 100, cutime => $info[15] / 100, cstime => $info[16] / 100, vsize => $info[22], rss => $info[23] * 4}; return $ref; }
Re: Determining the Memory Usage of a Perl program from within Perl
by princepawn (Parson) on Feb 21, 2001 at 05:26 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://58560]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2014-09-20 00:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (151 votes), past polls