Will Garbage Collection in .Net to collect COM memory.

  • Thread starter Thread starter John Sun
  • Start date Start date
J

John Sun

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
 
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.
 
John Sun said:
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.ReleaseCOMObject 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
 
Thanks, that makes me very clear!
Nicholas Paldino said:
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]
- (e-mail address removed)

John Sun said:
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
 
Back
Top