黑白析构函数和 Finalize 方法的区别
我想知道这些函数的调用顺序。就像如果我们的堆已满,GC 将被调用。它将标记该对象并调用其终结操作,现在我们有清理阶段..其中对该对象的引用被删除并且对象变得不可访问.. 那么,破坏在哪里循环进行……当它被称为……那么它在做什么……
I want to know the sequence of how these function are called. Like if our heap is full, GC will be called. It will mark the object and call its finalise operation, now we have sweep stage.. in which the reference to that object is deleted and object becomes inaccessible..
So where does the destruction come in cycle...When wld it be called..What wld it be doing then....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我必须推荐一下这个的来源。 C# 编译器团队的 Eric Lippert 最近就这个主题发表了一篇文章: 之间有什么区别析构函数和终结器?
从它们公认的定义来看,它们在 C# 中实际上是倒退的。读了他的文章,我无法更好地描述它。
I have to recommend the source on this one. Eric Lippert from the C# compiler team made a recent post on this exact subject: What’s the difference between a destructor and a finalizer?
From their accepted definitions, they're actually backwards in C#. Read his post though, I can't describe it any better.
C#/.NET 中没有“析构函数”,尽管该术语过去似乎与“终结器”互换使用。
大多数时候,您也不应该实现终结器;您需要使用它们的主要实例是类是否保留非托管资源,在这种情况下您应该实现 IDisposable 模式(从技术上讲,您可以调用 Dispose一个“析构函数”,但它实际上不释放堆上的任何内存,这是讨论内存管理时的一个重要区别)。
不要在内存管理方面对 GC 进行事后猜测。只需确保释放您使用的所有非托管资源(文件句柄等)。如果您看到有人使用术语析构函数,他们的意思可能是终结器。
There is no "destructor" in C#/.NET, although the term seems to have been used interchangeably with "finalizer" in the past.
Most of the time, you shouldn't be implementing finalizers either; the main instance where you need to use those is if the class holds on to unmanaged resources, in which case you should be implementing the
IDisposable
pattern (technically, you could callDispose
a "destructor", but it does not actually free any memory on the heap, which is an important distinction when discussing memory management).Don't second-guess the GC in terms of memory management. Just make sure you release any unmanaged resources you use (file handles, etc.). And if you see someone using the term destructor, they probably meant to say finalizer.
有很多很多文档解释了 .NET 垃圾收集器的工作原理。以下是一些可以开始的内容:
http://msdn.microsoft.com/ en-us/magazine/bb985010.aspx
http://msdn .microsoft.com/en-us/library/ms973837.aspx
http://www.simple-talk.com/dotnet/.net-framework/understand-garbage-collection-in-.net/
There are many, many documents which explain how the .NET garbage collector works. Here are a few to start with:
http://msdn.microsoft.com/en-us/magazine/bb985010.aspx
http://msdn.microsoft.com/en-us/library/ms973837.aspx
http://www.simple-talk.com/dotnet/.net-framework/understanding-garbage-collection-in-.net/
关于 .Net 内存管理的非常好的视频位于 .Net 内存管理< /a>.它将帮助您消除对终结器以及何时调用、如何调用等方面的疑问。
A Very good video about .Net Memory management is at .Net Memory Management. It will help clear your doubts about finalizer and when it is called, how it is called, etc.