473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Memory Leak with WMI

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
4 6037
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
17
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
8
by: Adrian | last post by:
Hi I have a JS program that runs localy (under IE6 only) on a PC but it has a memory leak (probably the known MS one!) What applications are there that I could use to look at the memory usage of...
7
by: Salvador | last post by:
Hi, I am using WMI to gather information about different computers (using win2K and win 2K3), checking common classes and also WMI load balance. My application runs every 1 minute and reports...
3
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". ...
7
by: Ragnar Agustsson | last post by:
Hi all I have been wandering about the best way to sandbox memory leaks in 3rd party libraries when using them from the .Net framework. I have a 3rd party library, written in C++, that leaks a...
22
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.