By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,796 Members | 1,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,796 IT Pros & Developers. It's quick & easy.

Memory Leak with WMI

P: n/a
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...b;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
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

--
Nathan

"Don Nell" <do*****@nospam.com> wrote in message
news:KbNZd.136400$tl3.101953@attbi_s02...
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...b;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

Nov 16 '05 #2

P: n/a
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.
"Nathan Neitzke" <ne******@erau.edu> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.

--
Nathan

"Don Nell" <do*****@nospam.com> wrote in message
news:KbNZd.136400$tl3.101953@attbi_s02...
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...b;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


Nov 16 '05 #3

P: n/a

"Don Nell" <do*****@nospam.com> wrote in message
news:y_TZd.73015$r55.15686@attbi_s52...
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...b;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.


Nov 16 '05 #4

P: n/a
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

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:eZ**************@TK2MSFTNGP15.phx.gbl...

"Don Nell" <do*****@nospam.com> wrote in message
news:y_TZd.73015$r55.15686@attbi_s52...
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...b;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.

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.