Memory Leak with WMI

D

Don Nell

Hello

Why is there a memory leak when this code is executed.

for(;;)
{
ManagementScope scope = new ManagementScope();
scope.Options.Username="username";
scope.Options.Password="password";
scope.Path.Path=@"\\pc\root\cimv2";
scope.Connect();

}

There is no leak if the username and password is blank and the computer is
in a domain. At first I thought it had something to do with Windows'
inablility to release more than 9 security context entries per 10 seconds
(see link below), but the leak still exist when I slow it down.

http://support.microsoft.com/default.aspx?scid=kb;en-us;890196

I also tried calling RpcMgmtEnableIdleCleanup() at startup and also forcing
any unused memory back to the operating system with SetProcessWorkingSetSize
but nothing helps. The Windows task manger always shows my application
consuming more and more memory until it finally crashes.

Thanks.

Don
 
N

Nathan Neitzke

It is really hard to tell from that small of a code snippet. But you have
an endless loop there, right? Well, in that case you are creating infinite
ManagementScope objects theoretically. Practically, when the garbage
collector is run will be the only time objects are cleaned up. So it would
make sense that if you continually instantiate new objects that your app
would increase in memory size?

Like I said, there is probably more to the picture here, I just don't get it
from the code snippet you provided.

Lastly, as far as when it crashes - what kind of exception do you get when
it does crash?

Take care.
 
D

Don Nell

Hello Nathan,

Thanks for your response. I first noticed the memory leak in a larger
application I was working on written in C++. I then realized that the leak
can be reproduced in every programming language I tried with a minimal
amount of code. I've included examples in VB, C++, and Delphi below. It all
seems to center around the "Connect" or "ConnectServer" call if the username
and password is not blank. The amount of the memory leak is always the same
at around 4Kb per connection.

If the computer running this code is on a domain and can log into the remote
computer using default security (without specifying username and password),
then there is no memory leak even if the username and password is specified.
If not, the memory consumption continues to climb until it either crashes
with an 'access violation' or it simply disappears with the following alert
message.

***************************************************
Windows - Virtual Memory Minimum too low
Your system is low on virtual memory. Windows is
increasing the size of your virtual memory paging
file. During this process, memory requests for
some applications may be denied. For more information,
see help.
***************************************************

Thanks again for your help.

Don

'**********************************************************
'VB Example
'**********************************************************
Do
Set locator = CreateObject("wbemscripting.swbemlocator")
Set oWMI = locator.connectserver("wimc0964", "root/cimv2", "administrator",
"system2")
locator = Null
oWMI = Null
Loop



//***********************************************************
C++ Example */
//**********************************************************
while(1==1)
{
CoInitializeEx(0, COINIT_APARTMENTTHREADED) ;
CoInitializeSecurity
(
NULL, -1, NULL, NULL,
RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL, EOAC_NONE, 0
) ;

ISWbemLocator *t_Locator = NULL ;
HRESULT t_Result = CoCreateInstance (

CLSID_SWbemLocator ,
NULL ,
CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER ,
IID_ISWbemLocator,
( void ** ) & t_Locator
) ;

ISWbemServices *t_Service = NULL ;

t_Result = t_Locator->ConnectServer (

L"computername" ,
L"root\\cimv2",
L"username" ,
L"password" ,
NULL ,
0 ,
NULL,
NULL,
&t_Service
) ;

t_Service->Release () ;
t_Locator->Release () ;
CoUninitialize();

}



//***********************************************************
//Delphi Example - Call Make Connection in a loop
//***********************************************************
unit WmiDelphiTest;

interface

uses Wbemads;

procedure MakeConnection ();

implementation

procedure MakeConnection ();
var
WbemLocator: ISWbemLocator;
pService: ISWbemServices;
begin

WbemLocator := CoSWbemLocator.create;
pService := WbemLocator.ConnectServer('MachineName', 'root\cimv2',
'UserName', 'Password', '', '', 0, pService);

end;

end.
 
W

Willy Denoyette [MVP]

Don Nell said:
Hello Nathan,

Thanks for your response. I first noticed the memory leak in a larger
application I was working on written in C++. I then realized that the leak
can be reproduced in every programming language I tried with a minimal
amount of code. I've included examples in VB, C++, and Delphi below. It
all
seems to center around the "Connect" or "ConnectServer" call if the
username
and password is not blank. The amount of the memory leak is always the
same
at around 4Kb per connection.

If the computer running this code is on a domain and can log into the
remote
computer using default security (without specifying username and
password),
then there is no memory leak even if the username and password is
specified.
If not, the memory consumption continues to climb until it either crashes
with an 'access violation' or it simply disappears with the following
alert
message.

***************************************************
Windows - Virtual Memory Minimum too low
Your system is low on virtual memory. Windows is
increasing the size of your virtual memory paging
file. During this process, memory requests for
some applications may be denied. For more information,
see help.
***************************************************

Thanks again for your help.

Don

'**********************************************************
'VB Example
'**********************************************************
Do
Set locator = CreateObject("wbemscripting.swbemlocator")
Set oWMI = locator.connectserver("wimc0964", "root/cimv2",
"administrator",
"system2")
locator = Null
oWMI = Null
Loop



//***********************************************************
C++ Example */
//**********************************************************
while(1==1)
{
CoInitializeEx(0, COINIT_APARTMENTTHREADED) ;
CoInitializeSecurity
(
NULL, -1, NULL, NULL,
RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL, EOAC_NONE, 0
) ;

ISWbemLocator *t_Locator = NULL ;
HRESULT t_Result = CoCreateInstance (

CLSID_SWbemLocator ,
NULL ,
CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER ,
IID_ISWbemLocator,
( void ** ) & t_Locator
) ;

ISWbemServices *t_Service = NULL ;

t_Result = t_Locator->ConnectServer (

L"computername" ,
L"root\\cimv2",
L"username" ,
L"password" ,
NULL ,
0 ,
NULL,
NULL,
&t_Service
) ;

t_Service->Release () ;
t_Locator->Release () ;
CoUninitialize();

}



//***********************************************************
//Delphi Example - Call Make Connection in a loop
//***********************************************************
unit WmiDelphiTest;

interface

uses Wbemads;

procedure MakeConnection ();

implementation

procedure MakeConnection ();
var
WbemLocator: ISWbemLocator;
pService: ISWbemServices;
begin

WbemLocator := CoSWbemLocator.create;
pService := WbemLocator.ConnectServer('MachineName', 'root\cimv2',
'UserName', 'Password', '', '', 0, pService);

end;

end.


All of your code samples create connections in a closed loop using explicit
cedentials, this results in a RPC context leak as described in
http://support.microsoft.com/default.aspx?scid=kb;en-us;890196.
When using implied credentials the same security context is used for every
new connection (the connection is re-used bo be exact), that's why you don't
see a leak.

Note that this is not a .NET nor a WMI issue, so I would suggest you apply
the patch and try again.

Willy.
 
D

Don Nell

I called Microsoft today and installed their hotfix but the problem still
remains. I also see a leak when the username and password are blank but it
is much slower.

Thanks anyway Willy for the suggestion.

Don
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top