对象无论我是否调用Dispose()? 在这里调用Dispose()之间的区别是什么? 调用Dispose()? 感谢您清除我的困惑... 摆动Hi Everyone,I''ve read most of the posts regarding "Dispose" in this NG.I still have some questions.If I declare and use a disposable object (say a DB connection) withina method, is it worth calling Dispose() within my "finally" block,even if that object only has local scope?When the thread exits this method, won''t the GC collect my disposableobject regardless of whether I call Dispose() or not?What would be the difference between calling Dispose() here and notcalling Dispose()?Thanks for clearing my confusion...Wobbles推荐答案 wobbles< no *************************** @ nospam.hotmail.co m>写道:wobbles <no***************************@nospam.hotmail.co m> wrote:我已经阅读了大部分关于Dispose的帖子。在这个NG中。我还有一些问题。 如果我在一个方法中声明并使用一次性对象(比如数据库连接),是否值得调用Dispose(在我的终于中阻止,即使该对象只有本地范围? 绝对 - 虽然我会使用using()块。 当线程退出此方法时,不会让GC收集我的一次性对象无论我是否调用Dispose()? 编号GC不会在每种方法结束时运行 - 它会在它需要的时候运行 。 在这里调用Dispose()和调用Dispose()之间的区别是什么? I''ve read most of the posts regarding "Dispose" in this NG. I still have some questions. If I declare and use a disposable object (say a DB connection) within a method, is it worth calling Dispose() within my "finally" block, even if that object only has local scope?Absolutely - although I''d use a using() block. When the thread exits this method, won''t the GC collect my disposable object regardless of whether I call Dispose() or not?No. The GC doesn''t run at the end of every method - it runs when itfeels it needs to. What would be the difference between calling Dispose() here and not calling Dispose()? 时间 - 如果调用Dispose(),所有适当的清理工作将在电话退回时完成,而不是在未来某些不确定的 点。 - Jon Skeet - < sk *** @ pobox.com> http://www.pobox.com/~skeet 如果回复小组,请不要给我发邮件Timing - if you call Dispose(), all the appropriate cleaning up will bedone by the time the call returns, rather than at some indeterminatepoint in the future.--Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeetIf replying to the group, please do not mail me too一次性对象是典型使用GC的资源,这些资源可能很难回收或者根本无法回收。通过调用Dispose(),你可以让对象知道它应该释放它的资源。它会这样做,并且 等待被GC吃掉。 如果你不打电话给Dispose()你就冒了用完的风险资源 (GC并不擅长清理那些)。 - 使用M2,Opera革命性的电子邮件客户端: http://www.opera.com/m2/ 笑一笑,尝试使用Opera的用户模式进行网页浏览并启用NostalgiaDisposable objects are objects that typicall use resources that the GC isslow to reclaim or may not reclaim at all. By calling Dispose() you letthe object know that it should release it''s resources. It will do so, andwait to be eaten by GC.If you don''t call Dispose() you run the risk of running out of resources(GC isn''t very good at cleaning up those).--Using M2, Opera''s revolutionary e-mail client: http://www.opera.com/m2/For a laugh, try web browsing with Opera''s User Mode and Nostalgia enabled wobbles写道:wobbles wrote:如果我在一个方法中声明并使用一次性对象(比如一个DB连接),是否值得在我的finally中调用Dispose()。阻止,即使该对象只有本地范围? 是的。如果你不再需要这个对象,你应该总是调用Dispose。 当线程退出这个方法时,不管GC是否收集我的一次性对象,无论是否我是否打电话给Dispose()? If I declare and use a disposable object (say a DB connection) within a method, is it worth calling Dispose() within my "finally" block, even if that object only has local scope?Yes. You should ALWAYS call Dispose if you do not need this object anymore. When the thread exits this method, won''t the GC collect my disposable object regardless of whether I call Dispose() or not? GC只调用终结器,而不是Dipose()...... 对象如果允许这种情况,应该实现一个finilizer。 有些对象有IDisposable-Interface但是他们没有实现一个 终结器。 所以如果你不打电话Dispose,将永远不会释放资源(可能是 节目终止) - 问候 Jochen 你需要一个内存泄漏查找器吗? http://www.codeproject.com/tools/leakfinder.asp 这篇关于在方法中调用Dispose?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-01 18:10