简体   繁体   English

为什么调用析构函数后 Object 仍在 Memory 快照中

[英]Why Is Object Still In Memory Snapshot After Destructor Called

I'm investigating a memory leak and am wondering why after the destructor is called the object Count and Size doesn't change in the memory snapshot.我正在调查 memory 泄漏,我想知道为什么在析构函数被称为 object 之后,memory 快照中的计数和大小没有变化。 It does look like the Inclusive Size does go down, but not to zero.看起来包含大小确实降低了 go,但不为零。 There are two rows as the first is a view and the second is its view model.有两行,第一行是视图,第二行是视图 model。

与析构函数断点命中前相比的内存快照

Can anyone explain why this happens?谁能解释为什么会这样? Thank you in advance.先感谢您。

EDIT: I just took another memory snapshot after writing this question up (maybe ~5 minutes later) and it seems the objects are now gone so maybe it just takes some time for everything to go away like longer than a few seconds after the destructor?编辑:在写完这个问题后(可能大约 5 分钟后),我刚刚拍摄了另一个 memory 快照,看起来这些对象现在已经消失了,所以可能只需要一些时间才能将 go 的一切都移开,就像在析构函数之后超过几秒钟?

Can anyone explain why.谁能解释为什么。 . . . . the destructor is called the object Count and Size doesn't change in the memory snapshot析构函数称为 object 计数和大小在 memory 快照中不会改变

.NET doesn't have destructors. .NET 没有析构函数。 It has finalizers which are only indirectly related to memory management.它具有仅与 memory 管理间接相关的终结器。 An object with a finalizer cannot be garbage collected until after the finalizer is run (or you call GC.SupressFinalize ).一个带有终结器的 object 在终结器运行(或调用GC.SupressFinalize )之前不能被垃圾收集。 But the object is not immediately garbage collected after its finalizer runs.但是 object 在其终结器运行后不会立即进行垃圾收集。

When GC finds an (otherwise) unreachable object with a finalizer a reference to the object is put on the freachable queue, and the finalizer thread runs the finalizer on all the objects that end up there.当 GC 发现带有终结器的(否则)无法到达的 object 时,将对 object 的引用放在可访问队列中,并且终结器线程对最终到达那里的所有对象运行终结器。 The object will be eligible for garbage collection on the next GC run after its finalizer runs. object 将有资格在其终结器运行后的下一次 GC 运行中进行垃圾收集。 Note that since the object has already survived a GC it will be at least on Gen1.请注意,由于 object 已经在 GC 中幸存下来,因此至少在 Gen1 上。 And if it's on Gen2 it may be a while until it's collected.如果它在 Gen2 上,它可能需要一段时间才能被收集起来。

The decision to reuse C++ destructor syntax for C# finalizers was probably a mistake.为 C# 终结器重用 C++ 析构函数语法的决定可能是一个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM