473,397 Members | 2,068 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,397 software developers and data experts.

Will Garbage Collection in .Net to collect COM memory.

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
3 4543
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

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

Similar topics

21
by: Rabbit63 | last post by:
Hi: I want to show a set of records in the database table on the clicnt browser. I have two ways to do this (writen in JScript): 1.The first way is: <% var sql = "select firstname from...
6
by: Ganesh | last post by:
Is there a utility by microsoft (or anyone) to force garbage collection in a process without have access to the process code. regards Ganesh
14
by: Ă–mer KUL | last post by:
hi, i have a garbage collection problem. i have an application writen by vb.net, i am opening a form and in this form there is a big query. when i open the form, it takes 80 mb memory because of...
3
by: Ian Taite | last post by:
Hello, I'm exploring why one of my C# .NET apps has "high" memory usage, and whether I can reduce the memory usage. I have an app that wakes up and processes text files into a database...
4
by: Pedro Miguel Carvalho | last post by:
Greetings. I'm creating a project that as a intricate relation between object kind of like a set where each object in the set can be connect to a subset of object of the set and objects not in...
4
by: klar02 | last post by:
We have a web server with 2GB of physical memory on it and we are seeing the memory usage running over 800,000KB in Task Manager. We have seen instances where the memory being this high(due to...
2
by: roger.dunham | last post by:
I am trying to identify whether a .NET 1.1 application that I have written has a memory leak. I thought I understood how .NET memory management worked, but it appears that there is more to it...
109
by: jacob navia | last post by:
In an interviw with Dr Dobbs, Paul Jansen explains which languages are gaining in popularity and which not: <quote> DDJ: Which languages seem to be losing ground? PJ: C and C++ are definitely...
158
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.