- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
因为 finalizer/IDisposable
和所谓的“IDisposable
模式”主题往往会引发大量故作姿态、武断和好战的观点(不是-分别是 here 、 here 、 here 等等),我真的很犹豫要问这个问题。希望先发制人那些老生常谈的辩论,我一直在回答一个非常简单的问题,这个问题在 StackOverflow 上似乎没有一个简洁的答案......
一旦对象的终结器开始执行,调用 GC.SuppressFinalize(this)
是否空洞?更具体或更有用(当然),从终结器本身内部调用 GC.SuppressFinalize(this)
是否无害? (同样,我们不在这里讨论任何“为什么”)
换句话说,除了调用 API 的开销以及在对象 header 中适当设置标志之外,是否存在任何不良、不需要或其他有形的正确性或性能影响?
最佳答案
当然,完全避免终结器并使用 SafeHandle
会更好,正如现代习语所要求的那样。然后所有这些关于终结器的东西都变得毫无意义。
也就是说,尽管这样做很明智,但从终结器调用 GC.SuppressFinalize()
是绝对安全的。 The documentation for the method描述该方法的作用:
This method sets a bit in the object header of obj, which the runtime checks when calling finalizers.
运行时实际上也可能在 GC 操作期间检查此位,即当发现无法访问的对象时,该对象的终结器将被放入终结器队列。如果在那个时候设置,finalizer 甚至不会在队列中结束。
稍后再次检查它,在调用终结器本身之前,也可以避免对象的终结,如果结果是某个其他对象的终结器最终处置了它,即使该对象的终结器已放入终结队列中。
这两项检查都发生在调用终结器之前。一旦调用终结器,对象中的位就没有用处了。设置它是无害的,但不会完成任何事情。
顺便说一句:请注意,过去的 .NET 实现使用 Finalizer
和 FReachable
队列。当一个对象被创建时,如果它有一个终结器,它将被移动到 Finalizer
队列中。一旦对象不可访问,它将被移动到 FReachable
队列以供稍后完成。调用 SuppressFinalize()
将从 Finalizer
队列中删除对象。到终结器运行时,对象不再在此队列中,因此 SuppressFinalize()
调用将是 NOP,同样无害。
现在,也就是说,您的问题很宽泛:“……是否存在任何不好的、不需要的或其他有形的正确性或性能影响?”。其中大部分是旁观者的眼睛。我认为调用 GC.SuppressFinalize()
的终结器是不正确的。所以,这对我来说是一个“有形的正确性效果”。我还发现偏离已发布的、公认的标准模式的代码是“不需要的”。如果问题中没有更具体的标准来约束它,那部分问题的答案可以是"is"、“否”、“有时”等中的任何一个。
事实上,您的问题是重复的,但没有人愿意回答:Calling GC.SuppressFinalize() from within a finalizer .不过,我确实找到了评论线索,尤其是埃里克·利珀特 (Eric Lippert) 的贡献:
Your supposition is that the unnecessary call to SuppressFinalize is the error in your plan. That's not the problem; the problem is the disposal of managed resources on the finalizer thread. Recall to your mind that finalizers run on their own thread, and that managed resources can be thread-affinitized, and now start to imagine the horrors that could result. Moreover: finalizers run in arbitrary order. A managed object disposed on the finalizer thread might have already been finalized; now you are possibly running finalization logic twice on one object; is it robust to that scenario? – Eric Lippert Mar 31 '16 at 21:58 1
Writing a correct finalizer is extraordinarily difficult and I recommend against you trying ever, ideally, but definitely hold off until you understand the pattern better. If you are not sufficiently scared yet, my series of articles on the subject might put more fear into you: ericlippert.com/2015/05/18/… – Eric Lippert Mar 31 '16 at 21:59
…
@Tom: The question is "I'm using the dispose pattern completely wrong; is this particular part of what I'm doing wrong?" No, the whole thing is wrong from the very first sentence. You don't use Dispose to dispose managed resources, and you certainly don't use a finalizer for that. That's the problem here. Is there anything wrong, per se, with calling SuppressFinalize from a finalizer? Well, it will work, but there should not be a situation in which that is the correct thing to do, so whether it works or not should be irrelevant. – Eric Lippert Jul 7 at 14:17
@Tom: Also, why do you call SuppressFinalize in the first place? Only because it is a performance optimization. But under what circumstances is it an optimization when called from the finalizer thread? Only when you've failed to make that optimization from the main thread! That's the place to do that optimization! – Eric Lippert Jul 7 at 14:24
恕我直言,这些评论将主要问题带到了一个很好的角度:询问从终结器调用 SuppressFinalize()
是否安全是错误的问题。如果您已经到了不得不问这个问题的地步,那么代码就已经错了,而且问题的答案可能并不那么相关。正确的方法是修复代码,这样您就不必问这个问题。
最后,虽然不是完全相同的问题,但我认为还值得指出的是,通常的指导是在 Dispose()
的末尾调用 SuppressFinalize()
方法可能不正确。如果调用,则应在 Dispose()
方法的开始 处调用。参见 Be Careful Where You Put GC.SuppressFinalize
关于c# - 在终结器中调用 GC.SuppressFinalize 是否无害?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58044679/
在我们对延迟敏感的应用程序中,我们有缓存数据(驻留在 TG 中)和在 YG 中消亡的短暂对象。我已经看到次要 GC 时间和主要 GC 时间有显着差异。我怀疑这与TG的尺寸相对较大有关。谁能解释 GC
我看到了多个建议运行 GC.Collect(GC.MaxGeneration) 的答案。 既然方法GC.Collect()会收集所有存在的分代,那么两者有什么区别吗? 也许如果只有两代而不是三代,GC
我们正在使用 UseParallelGC。 GC 日志看起来像 2016-06-09T19:38:17.362+0000:655312.397:[完整GC(人体工程学)[PSYoungGen:2291
我最近看到了两个非常好的和有教育意义的语言讲座: This first one由 Herb Sutter 撰写,介绍了 C++0x 的所有漂亮和酷炫的特性,为什么 C++ 的 future 似乎比以往
我们正在运行 gerrit 2.10.7,我们偶尔会遇到损坏的对象没有被 gerrit gc 修复的问题,即使 git gc 可以很好地修复它们。 另一方面,我读到 gerrit gc 会创建优化其他
我试图避免 Full GC(来自下面的 gc.log 示例)在生产中的 Tomcat 中运行 Grails 应用程序。关于如何更好地配置 GC 有什么建议吗? 14359.317:[完整 GC 143
我试图通过在析构函数中使用 console.WriteLine() 来确保释放某个类的实例,但输出从未出现。 我仔细搜索了任何挥之不去的引用资料以及事件订阅,但没有找到。只是为了我自己的理智,在我继续
之前看过一篇文章,说FGC影响时序,导致application出错结果。 代码示例如下: long start = System.currentTimeInMillis(); doSomething(
在 Java 中,我们可以使用 System.gc() 方法来建议 GC。今天我从this link开始了解C#中的GC.Collect()方法。 . 但我对解释有些不清楚。 第一行。 Forces
我理解 Python GC 有两种工作方式: 1) 基本引用计数 - 当“name”设置为“Tom”时,“John”下方的引用计数为零 name = "John" name = "Tom" (Refe
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
今天我们使用并发标记清除,具体如下: -XX:+UseConcMarkSweepGC 我看到一些文章推荐使用这种形式的附加参数: -XX:+UseConcMarkSweepGC -XX:+CMSInc
当我运行我的程序时,logcat 显示很多 GC Activity 喜欢 GC freed 10324 objects/ 510376 bytes in 103 ms GC freed 10324 o
2013-11-26T10:19:30.011+0800: [GC [ParNew: 2432484K->19997K(2696640K), 0.0378270 secs] 5560240K->315
在执行 GC 时,JVM 会遍历 Activity 对象,并清除未标记的对象。 根据: How to Tune Java Garbage Collection “Full GC的执行时间相对Minor
我有一个分布式缓存应用程序(内存绑定(bind),由于与集群中其他节点的交互而具有网络 I/O)在 JVM 1.7.0_51 中运行,带有 G1 垃圾收集器。这是 JVM 配置: -server -X
首先,我想让您知道,这是一个理论问题而不是实际问题,我只是好奇弱引用对象是如何被释放的。让我们快速记住 Java 中的弱引用是什么。粗略地说WeakReference意味着当没有指向“我”的强引用时,
这是运行大约 10 分钟后的输出。 Heap PSYoungGen total 7040K, used 0K [0x24060000, 0x247c0000, 0x26790000)
我正在运行一个应用程序,在 Weblogic 上使用 java 5 和 CMS 垃圾收集器。在垃圾收集日志中,我看到了消息日志,其中大部分消息我可以使用 Sun 的 Java HotSpot 虚拟机中
我有一个 ConcurrentMap> map = new ConcurrentHashMap>(); 并且希望当 SoftReference 的引用被 GC 时从映射中删除键/值对。 我该如何实现这
我是一名优秀的程序员,十分优秀!