Garbage collection and unmanaged resource
-
Friday, August 10, 2012 8:48 AM
Hi there,
I have a C# application which loads a C++ dll.
Is it possible that the garbage collection collect a memory block which is used by C++?
Thanks in advance.
All Replies
-
Friday, August 10, 2012 9:11 AM
Yes, if the C++ keeps a copy of a pointer passed to it, and uses that AFTER the function to which it was passed has returned.
The .Net marshalling code may pass a pointer to managed data directly to an unmanaged function, or it may make a copy of the data first and pass a copy of the data. Either way, the data passed will be "pinned" until the unmanaged function returns. However, once it has returned the managed data could be freed or moved in memory. So it is not safe for the unmanaged code to keep a copy of the pointer somewhere and use it later.
If you want to be able to do that, you will need to manually allocate the memory and mess around with the .Net marshalling methods to copy managed data into a manually-managed memory block. You then have control over when the memory block is freed, making it safe to pass to unmanaged code.
See this for more info:
http://msdn.microsoft.com/en-us/magazine/cc164193.aspx
Also google for Marshal.AllocHGlobal and GCHandle.Alloc
- Proposed As Answer by servy42Microsoft Community Contributor Friday, August 10, 2012 5:39 PM
- Marked As Answer by JupiterLee Monday, August 13, 2012 2:51 AM
-
Friday, August 10, 2012 9:53 AM
Hi,
It is possible to free the memory forcefully by calling GC.Collect, but it should be avoided because it may create performance issues.
If you are in the method/class where the object is still in scope, nothing can free up the memory. You then have to call Dispose() method on that object or make it go out of scope and after that, you can call the collect method.
For more description, check out :
GC.Collect Method
I hope this was helpful to you!
Thanks
- Marked As Answer by Jason Dot WangMicrosoft Contingent Staff, Moderator Monday, August 20, 2012 8:03 AM
-
Friday, August 10, 2012 10:26 AM
Please use Finalize and Dispose to Clean Up Unmanaged Resources
or
IDisposable.Dispose Method
Please check the following URLs for your reference
http://msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.71).aspx
http://msdn.microsoft.com/en-us/library/system.idisposable.dispose(v=vs.90).aspx
With Thanks and Regards
Sambath Raj.C
click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
Happy Programming!- Marked As Answer by Jason Dot WangMicrosoft Contingent Staff, Moderator Monday, August 20, 2012 8:03 AM
-
Friday, August 10, 2012 1:21 PM
i guess memory management by the clr uses same method to clear all the garbage collection. So when you call it, it will automatically clear the memory unused.
regards
joon
-
Saturday, August 11, 2012 11:30 AM
Hi,
In addition to other response it's a bit hard to give a general response. For example some win32 api have a function to clear the previously allocated memory buffer.
Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

