tags:

views:

515

answers:

6

What are the methods to destroy Excel COM Interop objects in C#, besides these:

object_instance = null;
System.GC.collect();
&
System.Runtime.InteropServices.Marshal.ReleaseComObject(object);

Please suggest only inbuilt techniques, not other tools like bigcannon, etc.

+13  A: 

The garbage collector is the only mechanism that can destroy a managed object, but you usually don't invoke it explicitly. You just let it do its thing.

Just like you never take your own trash to the depot, you just leave it sitting on the corner. It's always the garbage man's responsibility.

You can release references to things and clean them up with IDisposable, finalizers and destructors but not destroy them.

By using System.GC you can ask the garbage man to do things early - request a custom run just for yourself - but this usually screws up his schedule and he has a lot more trash to deal with than just yours so it's not recommended.

John K
(tidied up the interface name - hope you don't mind)
Marc Gravell
Not at all, but I was mid-edit, accidentally destroyed it but put it back.
John K
there are few casses where u need to explicitly delete the object .......although i am not able to show u my program but can assure you that i really need to destroy the object..
peril brain
"but this usually screws up his schedule and he has a lot more trash to deal with than just yours so it's not recommended. " the answers worth a +1 just for that - great answer anyway
RobV
@peril are you talking about a managed or an unmanaged object? As jdk said, use IDisposable for unmanaged objects. Explicitly Destroying managed objects is not possible to my knowledge, but may be a bit more details about *what* you want to destroy would be helpful. Just as a guess, if it's a security-related string that only needs to stay in memory very shortly, look at SecureString. Also, Streams (i.e. MemoryStream) can be explicitly disposed to my knowledge.
Michael Stum
THIS ANSWER IS REALLY A WORTH FOR +1..... U HAV'NT TOLD ME ANYTHING THAT MUCH USEFUL.. EXCEPT FOR A TEDIOUS LECTURE........
peril brain
@peril brain: I +1'd your comment because I see where you're coming from. Fortunately many others saw the usefulness of it answering your question about how you can "destroy" objects.
John K
+9  A: 

The kicker is that if you haven't dropped all references to the object, even GC.Collect won't destroy it.

The rule in C# (or .NET generally) is that you can't destory an object. Dispose() won't do it. A finalizer won't do it (rule number 2, don't use a finalizer unless you know why you need it, and don't ever call it directly).

For .NET, these are the things you need to do:

  1. If an object holds references to unmanaged objects, implement IDispose pattern. And implement it fully; it's more than just providing a Dispose() method.
  2. The only reason to have a finalizer is to release unmanaged objects, and only in the event that your object hasn't been properly disposed. Once your dispose method is called, it should do the cleanup, then "kill" the finalizer by calling GC.SuppressFinalize.
  3. If you are using an object that implements a Dispose() method, call Dispose() when you're done with that object. Best practices is to allocate that object with a using block. Exit from the using block will call Dispose() automatically.
  4. When you are done with an object, drop all references to the object, including event handlers, delegates, etc.
  5. Beyond that, trust the garbage collector to do its job. Don't mess with the garbage collector; you're only likely to make things worse, unless you really, really, really know what you're doing and why.
Cylon Cat
+1 - nice answer! I'd qualify #1 to include resources. If your class holds a reference to a managed object that implements IDispose, you also want to implement the Dispose Pattern. Generally that disposable class will represent a limited or expensive external resource.
TrueWill
@TrueWill, good advice. Thanks.
Cylon Cat
@Cylon Cat What about Marshal.ReleaseComObject?
Sergey Mirvoda
@Sergey, sorry, I'm pretty much clueless about COM issues.
Cylon Cat
A: 

If your object allocates significant resources, it's best to implement IDisposable and explicitly call Dispose. If you can't call Dispose for some reason and your class reserves unmanaged memory, you can use GC.AddMemoryPressure (with a matching GC.RemoveMemoryPressure in the finalizer) to tell the GC your class is heavier than it looks, prioritizing it for earlier cleanup.

Dan Bryant
+1  A: 

For the most part, you have to remove all references to an object. Only then will the garbage collector see it and destroy it.

Be aware of this:

object_instance = null;

All this does is kill the object_instance reference. This works if it's the only reference. If there are other references, then it won't be collected.

var skywalker = new Person();
var object_instance = skywalker;

...

object_instance = null;
//It's still alive and won't be collected because skywalker lives...
Aaron Daniels
A: 

If you really need to explicitly free the memory occupied by an object, and you are sure that you will choose the moment to exterminate it better than GC, then the only reasonable option is to allocate it on stack and let it die when you return from the method where it was allocated.

SK-logic
A: 

If you want control so that you can manage resources of the object, then implement the IDisposable interface.

http://msdn.microsoft.com/en-us/library/system.idisposable.aspx

If resources are not the reason why you want to explicitly control object destruction, I can't fathom why you want that level of control... Do you use object pooling?

code4life