garbage collection in C# and .NET

V

vinu

Hi All,

I am having a terrible problem with the garbage collector of the .NET
run-time.

My mangaged code(c#) application sends a System.String object to an unmanged
world COM client function
The COM client is exposed to my C# application by wrapping it in a class
with _gc keyword(e.g public _gc class DotNetWrapper).

In the Wrapper, the function returns a String*, which is nothing but a BSTR
returned from the COM client.

So ultimately in my C# application i have a pointer to a String object.
And as i send and receive response in a loop, garbage collection does not
seem to be happening and my
machine just takes more than 300MB and then hangs.

If i explicitly call System.gc.Collect(), even then memory cleanup is done
only randomly and not immediately.

If i write a C++ client to access my COM object, i can do a ::SysFreeString
on the BSTR to immediately free the memory.


Could anyone suggest a solution for this.

Thanks
Vinu
 
N

Nicholas Paldino [.NET/C# MVP]

vinu,

Why are you creating a managed wrapper for your pre-existing COM class?
If anything, you should use COM interop to handle the access to this, and
let the runtime handle marshalling the string to your client correctly (as
well as the disposal).

Hope this helps.
 

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