[prev in list] [next in list] [prev in thread] [next in thread] 

List:       perl-win32-users
Subject:    FW: Win32::OLE - Mem Leak?
From:       "Paul Sobey" <paul.sobey () gresearch ! co ! uk>
Date:       2006-03-24 9:10:08
Message-ID: 1601721959A84D4ABA8043B50D52A82012C408 () l3pmail1 ! uberit ! net
[Download RAW message or body]

Anybody got any ideas? Anybody from AS? Not sure what I can try next -
short of scheduling a restart of the service every week.

Paul
 

-----Original Message-----
From: perl-win32-users-bounces@listserv.ActiveState.com
[mailto:perl-win32-users-bounces@listserv.ActiveState.com] On Behalf Of
Paul Sobey
Sent: 20 March 2006 11:50
To: perl-win32-users@listserv.ActiveState.com
Subject: Win32::OLE - Mem Leak?

I'm trying to debug a slow memory leak in a service I have written to
monitor performance counters on my servers. The service queries several
wmi every 5 seconds, and exhibits a very slow leak, such that processes
grow to a 100MB in size after a few weeks.

I think I have pinned down the leak to the code which queries wmi. The
attached script, wmi.pl, demonstrates the problem, by successively
querying WMI and reporting its memory usage every hundred iterations. On
my machine (XP SP2, AP v. 5.8.8), this generates output similar to the
following:

000100 iterations, 0008011776 bytes, 0005795840 pages
000200 iterations, 0008036352 bytes, 0005300224 pages
000300 iterations, 0008052736 bytes, 0005349376 pages
000400 iterations, 0008052736 bytes, 0005349376 pages
000500 iterations, 0008056832 bytes, 0005357568 pages
000600 iterations, 0008056832 bytes, 0005357568 pages
000700 iterations, 0008056832 bytes, 0005357568 pages
000800 iterations, 0008056832 bytes, 0005357568 pages
000900 iterations, 0008060928 bytes, 0005357568 pages
001000 iterations, 0008060928 bytes, 0005357568 pages
001100 iterations, 0008060928 bytes, 0005357568 pages
001200 iterations, 0008065024 bytes, 0005365760 pages
001300 iterations, 0008065024 bytes, 0005365760 pages
001400 iterations, 0008065024 bytes, 0005365760 pages
001500 iterations, 0008065024 bytes, 0005365760 pages
001600 iterations, 0008069120 bytes, 0005365760 pages
001700 iterations, 0008069120 bytes, 0005365760 pages
001800 iterations, 0008069120 bytes, 0005365760 pages
001900 iterations, 0008073216 bytes, 0005373952 pages

... etc.

The commented lines 39-41 show my attempts to free the memory being
chewed up slowly, by freeing unused libraries, unloading the OLE system,
or sleeping to give it chance to garbage collect old objects - but the
script behaves the same whichever combination of these options is used,
as far as I can tell. I was hoping that when the $WMI objects went out
of scope they would be completely cleaned up, but this doesn't appear to
be the case.

Am I doing something wrong in my use of the module, or is this a genuine
bug?

Cheers,
Paul

*****************************************************************
Gloucester Research Limited believes the information 
provided herein is reliable. While every care has been 
taken to ensure accuracy, the information is furnished 
to the recipients with no warranty as to the completeness 
and accuracy of its contents and on condition that any 
errors or omissions shall not be made the basis for any 
claim, demand or cause for action.

The information in this email is intended only for the 
named recipient.  If you are not the intended recipient
please notify us immediately and do not copy, distribute 
or take action based on this e-mail.

Gloucester Research Limited, 5th Floor, Whittington House, 
19-30 Alfred Place, London WC1E 7EA
*****************************************************************

["wmi.pl" (application/octet-stream)]

use Win32::OLE;
use strict;

my $wmi_queries = {
        Win32_PerfRawData_PerfOS_Memory => [
                qw(
                    CommittedBytes
                    Frequency_Sys100NS
                    PageReadsPerSec
                    Timestamp_Sys100NS
                    )
        ],
};

my $count = 0;

while (1) {
        foreach my $class ( sort keys %{$wmi_queries} ) {
        my $WMI =
            Win32::OLE->GetObject("WinMgmts://$ENV{COMPUTERNAME}/root\\cimv2");                
                my $SQL = "SELECT "
                    . join( ", ", @{ $wmi_queries->{$class} } )
                    . " FROM $class";
                my $ResultSet = $WMI->ExecQuery($SQL);
                $count++;
                foreach my $result ( in $ResultSet) {
                        foreach my $attribute ( @{ $wmi_queries->{$class} } ) {
                                my $answer = $result->{$attribute};
                        }
                }
        } ## end foreach my $class ( sort keys...
        if ( $count % 100 == 0 ) {
                my ( $ws, $ppc ) = GetPidSize();
                printf(
                        "%s: %06d iterations, %010d bytes, %010d pages\n",
                        scalar localtime(time),
                        $count, $ws, $ppc
                );
                # sleep 1;                
                # Win32::OLE->FreeUnusedLibraries();
                # Win32::OLE->Uninitialize();
        } ## end if ( $count % 100 == 0)
} ## end while (1)

sub GetPidSize {
        my $WMI =
            Win32::OLE->GetObject("WinMgmts://$ENV{COMPUTERNAME}/Root\\CimV2");
        my $process_collection = $WMI->ExecQuery(
                "SELECT WorkingSetSize,PrivatePageCount
                FROM Win32_Process WHERE ProcessId=$$"
        );
        my $process = ( in $process_collection)[0];
        return ( $process->{WorkingSetSize}, $process->{PrivatePageCount} );
} ## end sub GetPidSize

["ATT103995.txt" (text/plain)]

_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic