Manually destroy C# objects

2019-01-07 18:19发布

I am fairly new to learning C# (from Java & C++ background) and I have a question about manual garbage disposal: is it even possible to manually destroy an object in C#? I know about the IDisposable interface, but suppose I'm dealing with a class which I didn't write and it doesn't implement it? It wouldn't have a .Dispose() method, so that and using { } is out, and .Finalize is always either protected or private so that's not an option either.

(I am just trying to learn what is possible in C# in this case. I suppose if all else fails I could inherit the hypothetical ImNotDisposable class so that it does implement IDisposable.)

8条回答
Juvenile、少年°
2楼-- · 2019-01-07 18:22

If the object is not reachable then you can call GC.Collect() and the object will be destroyed. The concept of IDisposable has nothing to do with the CLR and is mostly for user code to implement to perform additional disposal logic. Calling Dispose() on an object will not free the object itself from memory, though it may very well dispose any resources that this object references.

I should add that while what I said is a way to achieve this, in 99.9999% of applications you should never call GC.Collect() because it'll often degrade the performance of your application instead of improving it.

查看更多
SAY GOODBYE
3楼-- · 2019-01-07 18:24

You can't manually destroy an object "like C++ delete", all what you can do is just close any exclusive resources the object has acquired and null all references to this object, so the GC can collect it, also don't call GC.Collect() by yourself, the GC process is expensive, as it has to suspend all other threads to safely collect the objects from memory, so just trust the GC, and it will kick off when needed.

查看更多
做自己的国王
4楼-- · 2019-01-07 18:28

It's not possible to destroy an object in a deterministic fashion. The CLR determines when the flagged object is reclaimed. This means that while you can flag an object for reclamation and ensure that managed and unmanaged resources are tidied up for disposal (by implementing the IDisposable pattern), the actual time the memory is released is up to the CLR. This is different from C++ where you could actually delete something and it would be released then.

Happy coding,

Scott

查看更多
forever°为你锁心
5楼-- · 2019-01-07 18:31

No, you can't destroy a specific object.

It is possible to invoke the garbage collector, which will look for objects to destroy, but it's almost never a good idea.

查看更多
Animai°情兽
6楼-- · 2019-01-07 18:35

Although you can trigger garbage collection (you need to trigger GC for all generations because you can't be sure which generation the finalizable object is in) you cannot necessarily force finalization of a particular object. You can only depend upon assumptions about how the garbage collector works.

Furtnermore, since finalization happens on its own thread, you should call WaitForPendingFinalizers after triggering garbage collection.

GC.Collect(GC.MaxGeneration);
GC.WaitForPendingFinalizers();

As was noted by others, this can actually hurt your application's performance because unnecessarily invoking the GC can promote otherwise short-lived objects into higher generations which are more expensive to collect and are collected less frequently.

Generally speaking, a class that implements a finalizer (destructor) and does not implement IDisposable is frowned upon. And anything that implements IDisposable should be calling it's finalizer logic and supressing itself from finalization at garbage collection.

Jeff Richter recently posted a nice little trick for receiving a notification when garbage collection occurs.

Another great article on Garbage Collector Basics and Performance Hints by Rico Mariani (MSFT)

查看更多
唯我独甜
7楼-- · 2019-01-07 18:37

Say you have a class matrix and you created two matrix objects aMatrix and bMatrix. In C# you can manually destroy(finalize) an object like so:

aMatrix = NULL;

GC.Collect();

The garbage collector will notice that your aMatrix is NULL and will destroy (finalize) it. Whether or not this is a good idea is a different story.

查看更多
登录 后发表回答