为什么要调用Dispose()?不会发生内存泄漏? [英] Why call Dispose()? Memory leak won't occur?

查看:204
本文介绍了为什么要调用Dispose()?不会发生内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我的问题没有得到我所寻找的主要答案.我不清楚.我真的很想知道两件事:

Edit: My question isn't getting the main answer that I was looking for. I wasn't clear. I would really like to know two things:

  1. 不能调用Dispose()导致内存泄漏吗?
  2. 如果您有一个大型程序,并且从未在任何IDisposable对象上调用Dispose(),那会发生什么最糟糕的事情?
  1. Can NOT calling Dispose() cause memory leaks?
  2. What's the worst thing that can happen if you have a large program and never call Dispose() on any of your IDisposable objects?

给我的印象是,如果未在IDisposable对象上调用Dispose(),则可能发生内存泄漏.

I was under the impression that memory leaks could occur if Dispose() isn't called on IDisposable objects.

根据有关线程的讨论,我的看法是错误的;如果不调用Dispose(),则不会发生内存泄漏.

Per the discussion on this thread, my perception was incorrect; a memory leak will NOT occur if Dispose() isn't called.

那为什么还要打扰Dispose()呢?是否只是立即释放资源,而不是稍后释放资源?如果您有一个大型程序而从未在任何IDisposable对象上调用Dispose(),会发生什么最糟糕的事情?

Why ever bother calling Dispose() then? Is it just to free the resource immediately, instead of sometime later? What's the worst thing that can happen if you have a large program and never call Dispose() on any of your IDisposable objects?

推荐答案

Dispose用于释放非托管资源.如果一个类分配了非托管内存,这可能意味着内存,但更常见的是本机对象和资源,例如打开文件和数据库连接.

Dispose is used to release non-managed resources. This could mean memory, if a class allocated non-managed memory, but it is more often native objects and resources like open files and database connections.

您经常需要在本身没有任何非托管资源的类上调用Dispose,但是它确实包含另一个可抛弃的类,并且可能具有非托管资源.

You often need to call Dispose on a class that itself doesn't have any non-managed resources, but it does contain another class that is disposable and may have non-managed resources.

对于开发人员来说,实施处置以确保确定性的完成(有时确保释放资源的顺序)有时也很有用.

It's also sometimes useful for developers to implement dispose to ensure deterministic finalization--guaranteeing the order in which resources are freed.

还请注意,如果未调用Dispose,则实现dispose的类通常也具有终结器来释放资源.具有终结器的对象的生命周期与没有终结器的类的生命周期不同.当它们准备好用于GC时,GC将看到它们具有终结器,并且在GC准备就绪时不会立即收集对象,而是将其放入终结队列中.这意味着该对象可以进行一次额外的GC迭代.当您调用dispose时,实现通常(但不是必须)调用GC.SuppressFinalize(),这意味着不再需要调用终结器.

Also note that classes that implement dispose often also have a finalizer to release resourcdes if Dispose is not called. Objects with a finalizer have a different life-cycle than classes without one. When they are ready for GC, the GC will see that they have a finalizer and instead of immediately collecting the object when the GC is ready to, it puts it into the finalization queue. This means that the object lives for one extra GC iteration. When you call dispose, the implementation usually (but is not required to) calls GC.SuppressFinalize() which means the finalizer no longer needs to be called.

如果类实现了IDisposable,则应始终调用Dispose().

If a class implements IDisposable, you should always call Dispose().

这篇关于为什么要调用Dispose()?不会发生内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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