Difference between destructor, dispose and finalize method

I am studying how garbage collector works in c#. I am confused over the use of Destructor , Dispose and Finalize methods. As per my research and understandings, having a Destructor method within my class will tell the garbage collector to perform the garbage collection in the way mentioned in the destructor method which cannot be called explicitly on the instances of the class. The Dispose m

析构函数的区别,配置和终结方法

我正在研究垃圾收集器如何在c#中工作。 我对使用Destructor , Dispose和Finalize方法感到困惑。 根据我的研究和理解,在我的类中使用Destructor方法将告诉垃圾收集器按照析构函数方法中提到的方式执行垃圾收集,而该方法不能在类的实例上明确调用。 Dispose方法旨在为用户提供控制垃圾回收的功能。 Finalize方法释放类使用的资源,但不释放对象本身。 我不确定我是否以正确的方式理解它。 请澄清疑惑。 欢迎任何进一

Calling .Dispose() on a class that has a Finalizer

According to Essential C# 6.0 you should: AVOID calling Dispose() on owned objects that have a finalizer. Instead, rely on the finalization queue to clean up the instance. Could someone please elaborate on this as I'm not clear on what the point of Dispose is if we're not to call it from owned objects? Apart from Reflection, how would you figure out if the object has a Finalizer ?

在具有Finalizer的类上调用.Dispose()

根据Essential C#6.0,您应该: AVOID在拥有终结器的拥有对象上调用Dispose()。 相反,依靠最终化队列来清理实例。 有人可以请详细说明这一点,因为我不清楚Dispose的意义是什么,如果我们不从拥有的对象中调用它的话? 除了Reflection之外,你如何确定对象是否有Finalizer ? 除了搜索API文档(如果您有权访问它并存在)或反射之外,您如何确定何时调用Close() / Close() + Dispose() ? 我在网络上看到很多关于特

Do you need to dispose of objects and set them to null?

你是否需要处理对象并将它们设置为null,或者当垃圾收集器超出范围时将它们清理干净? Objects will be cleaned up when they are no longer being used and when the garbage collector sees fit. Sometimes, you may need to set an object to null in order to make it go out of scope (such as a static field whose value you no longer need), but overall there is usually no need to set to null . Regarding dispos

你需要处理对象并将它们设置为null吗?

你是否需要处理对象并将它们设置为null,或者当垃圾收集器超出范围时将它们清理干净? 对象将在不再使用时以及垃圾收集器看起来合适时清理。 有时,您可能需要将对象设置为null才能使其超出范围(例如,您不再需要的值的静态字段),但总体上通常不需要将其设置为null 。 关于处理对象,我同意@Andre。 如果对象是IDisposable , 那么当您不再需要时处理它是个好主意 ,特别是如果对象使用非托管资源。 不处置非托管资源将

How do I convince my colleagues not to implement IDisposable on everything?

I work on a project where there is a huge number of objects being instanced by a few classes that stay in memory for the lifetime of the application. There are a lot of memory leaks being caused with OutOfMemoryExceptions being thrown every now and again. It seems like after the instantiated objects ago out of scope, they are not being garbage collected. I have isolated the problem to being m

我如何说服我的同事不要在任何事情上实施IDisposable?

我在一个项目中工作,在这个项目中有大量的对象被一些在应用程序的整个生命周期中保留在内存中的类实例化。 有很多内存泄漏是由于OutOfMemoryException被一次又一次地抛出引起的。 看起来在实例化对象超出范围之后,它们不会被垃圾收集。 我把问题分离出来主要是关于附属于永远不会分离的长寿命对象的事件处理程序,从而导致长寿命对象仍然有超出范围对象的引用,这些永远不会被引用垃圾收集。 我的同事提出的解决方案如下

Why no Reference Counting + Garbage Collection in C#?

I come from a C++ background and I've been working with C# for about a year. Like many others I'm flummoxed as to why deterministic resource management is not built-in to the language. Instead of deterministic destructors we have the dispose pattern. People start to wonder whether spreading the IDisposable cancer through their code is worth the effort. In my C++-biased brain it seems

为什么没有引用计数+垃圾收集在C#中?

我来自C ++背景,并且我一直在使用C#大约一年。 像许多其他人一样,我为什么决定性的资源管理不是内置于语言中而感到沮丧。 我们有了处置模式,而不是确定性的析构函数。 人们开始怀疑是否通过他们的代码传播IDisposable癌症是值得的。 在我的C ++偏见的大脑中,似乎使用引用计数的智能指针与确定性析构函数是垃圾回收器的一个重要步骤,它需要您实现IDisposable并调用dispose来清理非内存资源。 诚然,我不是很聪明.....

Setting Objects to Null/Nothing after use in .NET

Should you set all the objects to null ( Nothing in VB.NET) once you have finished with them? I understand that in .NET it is essential to dispose of any instances of objects that implement the IDisposable interface to release some resources although the object can still be something after it is disposed (hence the isDisposed property in forms), so I assume it can still reside in memory or at l

在.NET中使用后将对象设置为Null / Nothing

一旦你完成了它们,你应该把所有的对象都设置为null (VB.NET中Nothing )? 我明白,在.NET中,处理实现IDisposable接口的对象的任何实例都是必要的,以释放一些资源,尽管该对象在处置后仍然是某些东西(因此表单中的isDisposed属性),所以我认为它可以仍然驻留在记忆中或至少部分? 我也知道,当一个对象超出范围时,它会被标记为垃圾收集器的下一个传递的收集(尽管这可能需要时间)。 因此,考虑到这一点,将它设置

Memory leak problems: dispose or not to dispose managed resources?

I experience strange memory leak in computation expensive content-based image retrieval (CBIR) .NET application The concept is that there is service class with thread loop which captures images from some source and then passes them to image tagging thread for annotation. Image tags are queried from repository by the service class at specified time intervals and stored in its in-memory cache (

内存泄漏问题:处置还是不处置管理资源?

我在运算昂贵的基于内容的图像检索(CBIR).NET应用程序中遇到了奇怪的内存泄漏 其概念是有线​​程循环的服务类捕获来自某个源的图像,然后将它们传递给图像标记线程进行注释。 服务类以指定的时间间隔从存储库查询图像标签,并将其存储在其内存中的缓存(字典)中以避免频繁的数据库命中。 该项目中的课程是: class Tag { public Guid Id { get; set; } // tag id public string Name { get; set; } //

Any sense to set obj = null(Nothing) in Dispose()?

Is there any sense to set custom object to null ( Nothing in VB.NET) in the Dispose() method? Could this prevent memory leaks or it's useless?! Let's consider two examples: public class Foo : IDisposable { private Bar bar; // standard custom .NET object public Foo(Bar bar) { this.bar = bar; } public void Dispose() { bar = null; // any sense? } } p

任何意义在Dispose()中设置obj = null(Nothing)?

在Dispose()方法中将自定义对象设置为null (在VB.NET中为Nothing )是否有意义? 这可以防止内存泄漏或无用?! 我们来看两个例子: public class Foo : IDisposable { private Bar bar; // standard custom .NET object public Foo(Bar bar) { this.bar = bar; } public void Dispose() { bar = null; // any sense? } } public class Foo : RichTextBox { // this could be als

Instance method call Dispose

Is correct that a public method calls the Dispose of IDisposable in the same class? Eg public class Worker : IDisposable { public void Close() { SendCloseCommand(); Dispose(); } public void Dispose() { ////other resources release } private void SendCloseCommand() { } } Another thing about disposable pattern: If the Close should be alw

实例方法调用Dispose

公共方法在同一个类中调用Dispose of IDisposable是否正确? 例如 public class Worker : IDisposable { public void Close() { SendCloseCommand(); Dispose(); } public void Dispose() { ////other resources release } private void SendCloseCommand() { } } 关于一次性模式的另一件事情:如果总是调用Close,那么从Dispose方法中调用的更好呢? 如果不

Can an IDispose objects not have an available Dispose method

I am using FileHelpers in one project, and the class MultiRecordEngine public sealed class MultiRecordEngine : EventEngineBase<object>, IEnumerable, IDisposable This class implements IDisposable , BUT don't have a public Dispose method... MultiRecordEngine eng = null; eng.Dispose(); // <---- Results in compilation error Inspecting this class code on GitHub I can see the metho

一个IDispose对象可以不具有可用的Dispose方法

我使用的一个项目FileHelpers,类MultiRecordEngine public sealed class MultiRecordEngine : EventEngineBase<object>, IEnumerable, IDisposable 此类实现IDisposable ,但没有公共的Dispose方法... MultiRecordEngine eng = null; eng.Dispose(); // <---- Results in compilation error 在GitHub上检查这个类的代码,我可以在这里看到第913行显式实现的方法: void IDisposable.Dispose() { Close();