Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Tracking memory of a running application

by DrWhy (Chaplain)
on Oct 24, 2005 at 15:13 UTC ( [id://502486]=perlquestion: print w/replies, xml ) Need Help??

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

Esteemed monks,

I'm trying to write a test script on Windows (XP/2003 Server) that will, among other things, track the memory usage of the appliation. I know that I can do this with an external tool like pslist from SysInternals, but would rather not have to drive an external utility and then parse it's output. Do any of you know of a module or some other way in Perl to do this? I've searched here and in CPAN but haven't found anything, unless maypbe Win32::Process::Info would do it, but that is not clear from it's documentation.

update: I thought I had made this clear originally, but upon seeing some of the responses and then re-reading my OP I see I left out a crucial piece of information: I'm tracking the memory used by an external c++ application that the Perl script spawns. So e.g., Devel::Size isn't going to do anything for me here. Sorry for the confusion.

--DrWhy

"If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

  • Comment on Tracking memory of a running application

Replies are listed 'Best First'.
Re: Tracking memory of a running application
by BrowserUk (Patriarch) on Oct 24, 2005 at 16:01 UTC

    Getting the info for the current process with Win32::Process::Info just requires you pass the current pid ($$) to GetProcInfo(). The hash returned (as the first element of the array) contains the following keys. I've indicated the keys that relate to the figures shown in the task manager/pslist:

    perl> use Win32::Process::Info;; perl> $p = Win32::Process::Info->new;; perl> @i = $p->GetProcInfo( $$ );; perl> $h = $i[0];; perl> print "$_ => ", $h->{ $_ } || 'n/a' for keys %$h;; TerminationDate => n/a QuotaNonPagedPoolUsage => 3800 ParentProcessId => 1172 Status => n/a OSName => Microsoft Windows XP Professional|C:\WINDOWS|\Device\Harddi +sk0\Partition1 CSName => D4KG9X0J PeakPageFileUsage => 5103616 PageFileUsage => 4186112 QuotaPeakNonPagedPoolUsage => 3920 WorkingSetSize => 3887104 .......## This is the tasklist "Mem Usage" +figure. CreationDate => 1130168062 HandleCount => 103 WindowsVersion => 5.1.2600 PeakWorkingSetSize => 7254016 MaximumWorkingSetSize => 1413120 PeakVirtualSize => 142602240 WriteOperationCount => 2 SessionId => n/a PrivatePageCount => 4186112 .....## This is the tasklist "VM Size" fi +gure Owner => D4KG9X0J\Me ProcessId => 188 Caption => perl.exe CommandLine => c:\perl\bin\perl.exe -sw "c:\Perl\bin\p1.pl" OSCreationClassName => Win32_OperatingSystem Priority => 8 MinimumWorkingSetSize => 204800 CreationClassName => Win32_Process ThreadCount => 7 KernelModeTime => 0.328125 ExecutionState => n/a CSCreationClassName => Win32_ComputerSystem InstallDate => n/a WriteTransferCount => 144 OtherTransferCount => 203274 PageFaults => 2758 VirtualSize => 142602240 QuotaPagedPoolUsage => 25776 OwnerSid => S-1-5-21-756011271-1152625135-639526082-1005 ReadTransferCount => 347129 OtherOperationCount => 3358 ReadOperationCount => 171 QuotaPeakPagedPoolUsage => 25808 ExecutablePath => c:\perl\bin\perl.exe UserModeTime => 0.265625 Name => perl.exe Description => perl.exe Handle => 188

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Great, thanks. PeakVirtualSize is actually what we are looking for.

      --DrWhy

      "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

        You are aware that the PeakVirtualSize includes a lot of memory that is shared between processes?

        For example, it will include things like kernel32.dll and user32.dll which are shared by virtually (sic) every program in the system. So, whilst every perl program will use them, they will never cause additional memory* to be used when a Perl script is run as they will already be in memory.

        With the exception of a few handles and stuff


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Tracking memory of a running application
by Elian (Parson) on Oct 24, 2005 at 22:27 UTC
    If you don't need exact size (that is, you only want to track the memory perl uses, and not the memory used by XS modules loaded, or libraries those modules use, and don't mind a little slop on top of that) you can use Devel::Size, hand it your top-level namespace pointer, and let it rip. It will use a pretty massive amount of memory itself as it runs, so don't be too surprised.

    Usage is:

    use Devel::Size qw(total_size); print total_size(*::), "\n";

      See Re: Total Memory Size Used up by a Perl Script for a little further info.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Huh. I thought I'd fixed the warnings that you've seen in one of the recent versions of Devel::Size, but maybe not.

        I've an outstanding patch for it, so maybe I'll poke at the thing a bit and push out a new release with fewer warnings.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://502486]
Approved 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: (4)
As of 2024-03-29 15:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found