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

Will Garbage Collection in .Net to collect COM memory.

P: n/a
Dear Group Gurus,

If I use a COM class in my C# code, will the memory used by COM object be
garbage collected, or do I have to manually collect it.

Thanks,
John
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
John,

You don't have to manually collect it, but you should release the object
as soon as possible. There are two things that are going on here that you
have to be aware of.

First, there is the actual COM object in memory. There is also the
Runtime Callable Wrapper (RCW) which is the managed representation of the
COM object.

When you pass the RCW (what you use in your .NET code) to the static
ReleaseComObject method on the Marshal class, the runtime callable wrapper
will decrement the reference count on the COM object, and remove the
association between the COM object and the wrapper. It is very possible
that this object will still survive if the reference count is higher than
one. However, this isn't really your concern, just like it isn't when
programming in the unmanaged realm. In the unmanaged realm, you are
responsible for releasing the references that you are responsible for. So,
in the same sense, call ReleaseComObject for the references you know you are
responsible for.

Then that leaves the RCW. However, once the reference to the COM object
is released, this object is like any other, and has no unmanaged resource
associated with it. Assuming no other .NET object has a reference to it, it
will be garbage collected.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"John Sun" <JS***********@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Dear Group Gurus,

If I use a COM class in my C# code, will the memory used by COM object be
garbage collected, or do I have to manually collect it.

Thanks,
John

Nov 16 '05 #2

P: n/a

"John Sun" <JS***********@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Dear Group Gurus,

If I use a COM class in my C# code, will the memory used by COM object be
garbage collected, or do I have to manually collect it.


The memory used by the unmanaged COM class is unmanaged. It will be cleaned
up by the COM component itself when its interface reference count goes to
zero. Your C# code references the COM component through a Runtime Callable
Wrapper (RCW), which is a managed wrapper type but has an COM interface
pointer to one of the interfaces of the COM component.

The RCW has a finalizer which releases the COM interface pointer and
destroys the COM component. The finalizer will run sometime after the RCW
is garbage collected. In some scenarios this can lead to large amounts of
unmanaged memory remaining allocated for large periods of time.

If you have RCW's being promoted into Generation 2, this can be a huge
problem. Each RCW is tiny, but can hold onto large amounts of unmanaged
memory. Thousands and thousands of RCW's can languish in Gen2 for a very
long time before the managed heap gets large enough to trigger another
garbage collection.

There is a way to explicitly destroy a COM object when you are done with it.
System.Runtime.InteropServices.Marshall.ReleaseCOM Object will release the
interface pointer immediately. I would recommend that you always use it
when you're done with a COM object. The RCW's will magically destroy the
COM objects, but I wouldn't rely on it since there's no guarantee it will be
done in a timely manner.

David
Nov 16 '05 #3

P: n/a
Thanks, that makes me very clear!
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:OG**************@TK2MSFTNGP12.phx.gbl...
John,

You don't have to manually collect it, but you should release the object as soon as possible. There are two things that are going on here that you
have to be aware of.

First, there is the actual COM object in memory. There is also the
Runtime Callable Wrapper (RCW) which is the managed representation of the
COM object.

When you pass the RCW (what you use in your .NET code) to the static
ReleaseComObject method on the Marshal class, the runtime callable wrapper
will decrement the reference count on the COM object, and remove the
association between the COM object and the wrapper. It is very possible
that this object will still survive if the reference count is higher than
one. However, this isn't really your concern, just like it isn't when
programming in the unmanaged realm. In the unmanaged realm, you are
responsible for releasing the references that you are responsible for. So, in the same sense, call ReleaseComObject for the references you know you are responsible for.

Then that leaves the RCW. However, once the reference to the COM object is released, this object is like any other, and has no unmanaged resource
associated with it. Assuming no other .NET object has a reference to it, it will be garbage collected.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"John Sun" <JS***********@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Dear Group Gurus,

If I use a COM class in my C# code, will the memory used by COM object be garbage collected, or do I have to manually collect it.

Thanks,
John


Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.