什么是GC,敲定()和Dispose的关系? [英] what is relation between GC, Finalize() and Dispose?

查看:241
本文介绍了什么是GC,敲定()和Dispose的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GC是管理对象并完成对非托管对象这就是我一直在读。处置是隐含的,是敲定明确的就是我一直在读。有人可以给我这三个东西都被用于不同的原因之一模块的一个例子吗?

GC is for managed objects and Finalize is for unmanaged objects thats what I have been reading. Dispose is implicit and Finalize is Explicit is what I have been reading. Can somebody give me one example of one module in which all three thing have been used for different reasons?

推荐答案

GC是垃圾收集。它是自动内存管理,处理在托管堆中分配的对象的清理。在.NET GC采用了标记和清除算法。当垃圾收集时,它基本上认为在堆的一部分的所有物体待清洁可收回。然后,它通过标记处理它扫描的根源。即它标识仍处于由应用程序使用的对象。做完剩余的对象有资格清理。堆可以被压制作为清理的一部分。

GC is garbage collection. It is the automatic memory management, that handles cleanup of objects allocated on the managed heap. The .NET GC employs a mark and sweep algorithm. When a garbage collection occurs it basically considers all object in the part of the heap to be cleaned as recoverable. Then it goes through a marking process where it scans for roots. I.e. it identifies objects that are still in use by the application. Having done that the remaining objects are eligible for cleanup. The heap may be compacted as part of the cleanup.

处置和释放方法都提供一个选项,清洁资源,正在的的气相色谱处理。例如。这可能是天然的手柄等。他们什么都没有做与在托管堆中回收内存。

Dispose and finalizer methods both offer an option for cleaning resources, that are not handled by GC. E.g. this could be native handles and the like. They have nothing to do with reclaiming memory on the managed heap.

处置必须明确地在其实施的IDisposable A型调用。它可以被称为既可以通过的Dispose()方法本身或通过使用结构。 GC将不会自动调用进行处理。

Dispose must be called explicitly on a type which implement IDisposable. It can be called either through the Dispose() method itself or via the using construct. The GC will not call Dispose automatically.

在另一方面终结或析构函数(如语言规范要求的话)会自动被调用的某个的后对象有资格进行清理。 finalize方法是在一个专用的线程顺序执行。

A finalizer or destructor (as the language specification calls it) on the other hand will automatically be called sometime after the object was eligible for cleanup. Finalize methods are executed sequentially on a dedicated thread.

的Dispose()允许资源的确定性清理,同时终结能的情况下,作为安全网行动的用户没有按'T呼叫的Dispose()

Dispose() allows deterministic cleanup of resources while a finalizer can act as a safety net in case the user doesn't call Dispose().

如果一个类型实现一个终结,实例的清理延迟为终结之前必须清理被调用。即这将需要额外的收集回收内存的类型的实例。如果类型实现IDisposable为好,Dispose方法可以调用,然后该实例可以从定稿中删除自身。这将允许待清洁物体,就好像它没有一个终结。

If a type implements a finalizer, cleanup of instances is delayed as the finalizer must be called prior to cleanup. I.e. it will require an additional collect to reclaim the memory for instances of the type. If the type implements IDisposable as well, the Dispose method can be called and then the instance can remove itself from finalization. This will allow the object to be cleaned up as if it didn't have a finalizer.

如果您想挖成这样,我推荐的通过C#CLR通过杰弗里里希特。这是一个伟大的书,它涵盖了这一切的血淋淋的细节(我离开了一些细节)。新的第3版刚刚发布。

If you want to dig into this, I recommend CLR via C# by Jeffrey Richter. It is a great book and it covers all the gory details of this (I left out a number of details). The new 3rd edition was just released.

这篇关于什么是GC,敲定()和Dispose的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆