强制垃圾收集在C#中的最佳实践 [英] Best Practice for Forcing Garbage Collection in C#

查看:173
本文介绍了强制垃圾收集在C#中的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的经验看来,大多数人会告诉你,这是不明智的强制垃圾回收,但在某些情况下,你正在处理并不总能收集到0代,但在那里的内存是一个大对象的问题,是它确定强制收取?是否有一个最佳实践在那里这样做?

In my experience it seems that most people will tell you that it is unwise to force a garbage collection but in some cases where you are working with large objects that don't always get collected in the 0 generation but where memory is an issue, is it ok to force the collect? Is there a best practice out there for doing so?

推荐答案

最好的做法是不强制进行垃圾回收。

The best practise is to not force a garbage collection.

根据MSDN:

这是可以强制垃圾   收集致电收集,但   大部分时间,这应该是   避免,因为它可能会产生   性能问题。

"It is possible to force garbage collection by calling Collect, but most of the time, this should be avoided because it may create performance issues. "

不过,如果你能可靠地测试您的code,以确认调用收集()将不会有负面影响,然后继续前进......

However, if you can reliably test your code to confirm that calling Collect() won't have a negative impact then go ahead...

只是尽量确保物品被清理,当你不再需要它们。如果你有自定义对象,看看使用了使用说明和IDisposable接口。

Just try to make sure objects are cleaned up when you no longer need them. If you have custom objects, look at using the "using statement" and the IDisposable interface.

该环节与方面一些好的实用的建议,以释放内存/垃圾收集等:

This link has some good practical advice with regards to freeing up memory / garbage collection etc:

<一个href="http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx">http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx

这篇关于强制垃圾收集在C#中的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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