C# app as Service - Causing Memory leak..

S

SPG

Hi,
We have a C# app that uis running as a service.
It uses a third party activeX dll that we us to capture events.
Because it is an activeX dll, we have to create our own message pump on a
thread (I Quite simple call application.run() on a new thread just after
creatign eh activeX component)..

All this works fine, but we have noticed that we are getting quite a memory
leak when running as a service.

Over the weekend, the application was running and this morning has consumed
300+ MB of ram. Normally sits in 5!

I have tried forcing GC every minute or so. This seems to help the situation
a bit.
Is there a known issue with ActiveX through interop and services?

Cheers,

Steve
 
N

Nicholas Paldino [.NET/C# MVP]

SPG,

I don't think that you should be calling Application.Run to establish
the threading model of the thread that the code is running on. If anything,
I would create a new thread in the OnStart method, and then set the
ApartmentState property of that thread to STA (or MTA, if you need it).
Then, on that thread, you would perform your work.

Also, are you calling the static ReleaseComObject on the Marshal class
to release all of your COM objects when you are done with them? You should
be aware that if one object you create exposes a property which exposes
another COM object, then you have to call ReleaseComObject on that as well.

Hope this helps.
 
B

B0nj

Also, are you calling the static ReleaseComObject on the Marshal class
to release all of your COM objects when you are done with them?

Can I just add that this means *ALL* com objects, i.e. including the ones
that get created in the CCW that you don't ever 'see'.
For instance, if you use Excel as such:

Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWbk = xlApp.Workbooks.Add();

you may be forgiven for thinking you've only requested two
COM objects - an Excel.Workbook and an Excel.Application,
hence if you release them, then Excel will be able to be freed.
But no - you've requested 3, an Excel.Workbooks collection
aswell.

You may need to put your code in more lines than you otherwise
would for a standard gc library - so you get references to *every*
COM object you create so that you can then destroy it explicitly.
The CCW *WON'T* do this for you!
 
S

SPG

Hi,

Thanks for the response.
OK, First things first..
If I do not call Application.Run(), then I do not get any events from my
activeX objects. I spent ages trying to make this work using a simple new
thread in Apratmentstate.STA etc, but until I called Application.Run() I
would not receive any events. (A rather sucky feature of .NET services I am
told).

Secondly, My ActiveX control is a server that simply pings me events in the
form of a byte array. I only ever create one instance of this object. The
event fires me what I assume to be an array of primitive types. I have also
made a sample app that uses a System.Windows.Form.Timer to fire events. This
passes a long value and this too leaks in service mode, so I am unsure if it
is anything to do with COM objects themselves, I am pretty certain there is
a leak with events.

Steve

Nicholas Paldino said:
SPG,

I don't think that you should be calling Application.Run to establish
the threading model of the thread that the code is running on. If anything,
I would create a new thread in the OnStart method, and then set the
ApartmentState property of that thread to STA (or MTA, if you need it).
Then, on that thread, you would perform your work.

Also, are you calling the static ReleaseComObject on the Marshal class
to release all of your COM objects when you are done with them? You should
be aware that if one object you create exposes a property which exposes
another COM object, then you have to call ReleaseComObject on that as well.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

SPG said:
Hi,
We have a C# app that uis running as a service.
It uses a third party activeX dll that we us to capture events.
Because it is an activeX dll, we have to create our own message pump on a
thread (I Quite simple call application.run() on a new thread just after
creatign eh activeX component)..

All this works fine, but we have noticed that we are getting quite a memory
leak when running as a service.

Over the weekend, the application was running and this morning has consumed
300+ MB of ram. Normally sits in 5!

I have tried forcing GC every minute or so. This seems to help the situation
a bit.
Is there a known issue with ActiveX through interop and services?

Cheers,

Steve
 
J

jerry

SPG wrote...
[snip]
Secondly, My ActiveX control is a server that simply pings me events in the
form of a byte array. I only ever create one instance of this object. The
event fires me what I assume to be an array of primitive types. I have also
made a sample app that uses a System.Windows.Form.Timer to fire events. This
passes a long value and this too leaks in service mode, so I am unsure if it
is anything to do with COM objects themselves, I am pretty certain there is
a leak with events.

http://samgentile.com/blog/archive/2003/04/17/5797.aspx

http://radio.weblogs.com/0111019/stories/2002/08/05/connectionPointBasedEv
entHandlingBetweenComAndnet.html
 
S

SPG

Hi,

Although these were very interesting write-ups, as I said before, I am only
creating one object via com and that object then sends me events. Each event
was leaking about 8bytes per call in win service mode. as an app it was
fine.

Finally I solved the problem by setting my main service thread to be
[MTAThread], then spinning of the worker thread in STA mode. This thread
does the creation of the COM object, and once all is running nicely calls
Application.Run() on the same thread to set up the event sink.

This all works great and although not the cleanest solution, solves my prob.

Cheers,

Steve
jerry said:
SPG wrote...
[snip]
Secondly, My ActiveX control is a server that simply pings me events in the
form of a byte array. I only ever create one instance of this object. The
event fires me what I assume to be an array of primitive types. I have also
made a sample app that uses a System.Windows.Form.Timer to fire events. This
passes a long value and this too leaks in service mode, so I am unsure if it
is anything to do with COM objects themselves, I am pretty certain there is
a leak with events.

http://samgentile.com/blog/archive/2003/04/17/5797.aspx

http://radio.weblogs.com/0111019/stories/2002/08/05/connectionPointBasedEv
entHandlingBetweenComAndnet.html
 

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