什么时候可以接受的调用GC.Collect? [英] When is it acceptable to call GC.Collect?

查看:422
本文介绍了什么时候可以接受的调用GC.Collect?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般的建议是,你不应该从你的code调用 GC.Collect的,但什么是例外情况?

The general advise is that you should not call GC.Collect from your code, but what are the exceptions to this rule?

我只能想一些非常特殊的情况下,它可能是有意义的强制垃圾回收。

I can only think of a few very specific cases where it may make sense to force a garbage collection.

的一个例子是弹簧想到是一种服务,该唤醒间隔,执行某些任务,然后睡了很长时间。在这种情况下,它可能是一个好主意,强制收集到prevent即将要被闲置的过程,从持有上比需要更多的内存。

One example that springs to mind is a service, that wakes up at intervals, performs some task, and then sleeps for a long time. In this case, it may be a good idea to force a collect to prevent the soon-to-be-idle process from holding on to more memory than needed.

还有没有其他的情况下是可以接受调用 GC.Collect的

Are there any other cases where it is acceptable to call GC.Collect?

推荐答案

如果您有充分的理由相信,一个显著组对象 - 特别是那些你怀疑是在代1和2 - 现在有资格进行垃圾回收,而现在是一个合适的时间来收集在小性能损失的条款。

If you have good reason to believe that a significant set of objects - particularly those you suspect to be in generations 1 and 2 - are now eligible for garbage collection, and that now would be an appropriate time to collect in terms of the small performance hit.

这方面的一个很好的例子是,如果你刚刚关闭了大量的形式。你知道,所有的UI控件现在可以被垃圾收集,并在很短的停顿作为可能是封闭的形式不会明显给用户。

A good example of this is if you've just closed a large form. You know that all the UI controls can now be garbage collected, and a very short pause as the form is closed probably won't be noticeable to the user.

这篇关于什么时候可以接受的调用GC.Collect?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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