我们如何使用垃圾收集器释放不使用的资源 [英] How we can I release unuse resources using garbage collector

查看:72
本文介绍了我们如何使用垃圾收集器释放不使用的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何使用代码中的垃圾收集器释放未使用的资源?

How we can release un-use resources using garbage collector from our code?

推荐答案

一般情况下,你没有。您可以通过调用GC.Collect激活GC,但它可能不是您实际需要做的。



相反,您应该在所有实现IDisposable接口您的类包含资源,并始终在实现它的所有类上调用Dispose或使用使用块。等待GC(甚至手动强制它激活)是一种非常糟糕的做法,也是一种非常低效的做事方式。
In general you don't. You can cause the GC to activate by calling GC.Collect, but it's probably not what you actually need to do.

Instead, you should implement the IDisposable interface on all your classes which contain resources, and always either call Dispose or use a using block on all classes which implement it. Waiting for the GC (or even manually forcing it to activate) is a very poor practice, as well as being a very inefficient way to do things.


调用GC.Collect()。



以下是参考资料



http://stackoverflow.com/questions/4257372/how-to-force-garbage-collector-to-run [ ^ ]

http://stackoverflow.com/questions/ 233596 /最佳实践 - 强制垃圾收集 - 急剧收集 [ ^ ]
Call GC.Collect().

Here are the reference

http://stackoverflow.com/questions/4257372/how-to-force-garbage-collector-to-run[^]
http://stackoverflow.com/questions/233596/best-practice-for-forcing-garbage-collection-in-c-sharp[^]


在.NET框架中,如果资源在托管堆中,GC自动处理它。

或者实现Dispose()可以满足你的目的。
In .NET framework, if the resource is in managed heap, GC takes care of it automatically.
Or implementing Dispose() can serve your purpose.


这篇关于我们如何使用垃圾收集器释放不使用的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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