调用Dispose方法会清除并压缩.Net中的内存吗?
我对 .Net 中的 Dispose 和 Finalize 方法有一个想法,如下所示。这是正确的吗?
Dispose :实现 IDisposable 接口并删除 Dispose()
方法中未使用/非托管的代码。如果开发人员想要立即删除,则需要手动调用它,否则 GC 将在调用时处理资源。
Finalize :当 GC 调用时,它将释放未使用的托管代码,如果实现了 IDisposable,那么它将调用 Dispose()
方法来释放非托管资源(通常)。
本质上,当我们使用 Dispose() 方法处置资源时,内存会立即释放并压缩(就像 GC 那样)吗?
I have an idea about Dispose and Finalize method in .Net as laid out below. Is this correct?
Dispose : Implement IDisposable interface and remove unused/unmanaged code in the Dispose()
method. The developer needs to call it manually if they want immediate removal or GC will dispose the resources when it is invoked.
Finalize : When GC invoked it will free the unused managed code and if IDisposable is implemented then it will call the Dispose()
method to free up the unmanaged resources (normally).
Essentially, when we dispose the resources using the Dispose()
method, will memory will be freed immediately and compacted (like the GC does)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题的答案是否定的:释放为对象分配的内存与调用
Dispose
方法无关。当垃圾收集器到达它时,它会在适当的时候发生。一般来说,
Dispose
旨在加速外部资源的释放,例如文件句柄、信号量、数据库句柄以及操作系统经常分配的其他项目。如果您的对象保留其他IDisposable
对象,它也应该在调用 dispose 时处置它们。然而,Finalizer 是不同的:它作为垃圾收集的一部分被调用,旨在释放在处置期间尚未释放的外部资源(可能是因为用户忘记调用
Dispose
)。终结器不得对您的对象可能持有的其他对象调用Dispose
,因为它们已经处于垃圾收集过程中。The answer to your question is no: releasing the memory allocated for the object has nothing to do with calling the
Dispose
method. It happens in due course when the garbage collector gets to it.Generally speaking,
Dispose
is intended for speeding up the release of external resources, such as file handles, semaphores, db handles, and other items often allocated by the operating system. If your object holds on to otherIDisposable
objects, it should dispose them in its call to dispose as well.Finalizer, however, is different: it is called as part of garbage collection, and is intended for releasing external resources that have not been released during the dispose (presumably, because the user forgot to call
Dispose
). Finalizers must not callDispose
of other objects that your object may hold, because they are in the process of being garbage collected already.不会。直接或通过 using 语句调用 Dispose 方法不会导致内存被释放。
实现 IDisposable 只会让你的类有机会清理它所持有的任何非托管资源。
No. Calling the Dispose method directly or via a using statement will not cause memory to be freed.
Implementing IDisposable will just give your class a chance to clean up any unmanaged resources its holding onto.
你在这里有些不正确。当你说“它会调用Dispose”时,如果你指的是GC本身,那么,不,它不会“自动”为你调用Dispose。作为程序员,您的工作就是在 Dispose 和 Finalizer 方法中进行清理。
MSDN 文章演示了典型的处置模式。
不,调用 Dispose 不会释放堆内存。直到 GC 运行并执行清理后,堆内存才会被释放。
You are somewhat incorrect here. When you say "it will call Dispose", if you are referring to the GC itself, then, no, it does not "automatically" call Dispose for you. It is your job as the programmer to do cleanup in both the Dispose and Finalizer methods.
This MSDN writeup here demonstrates the typical disposal pattern.
No, calling Dispose does not free heap memory. The heap memory is not released until the GC runs and performs the cleanup.