所以GC.Collect()GC、Collect

2023-09-02 11:47:29 作者:扎男

好吧,我读过一对夫妇关于它的话题,但在这里不言而喻。让我们想象一下我有一个应用程序,基本上每一个现在,然后我会点击一个按钮,很多事情会发生几分钟,然后它会赋闲可能一个小时,或者只是1分钟。难道只是整个结束后的良好局面,以调用GC.Collect?我的意思是,我不知道,就在那一刻,我不会被使用我的应用程序,和GC不能猜测它。 解决方案

我可以看到,几个人都走了极端有关不推荐给调用GC.Collect。

GC.Collect的是有原因的,这里有我的时候,为什么要调用GC.Collect的推荐。

在通用,不用担心调用它,GC曲调本身非常好,会做正确的事情。

有时候,你就会在你肯定知道这是一个正确的时间来调用它的情况,你上述的情况是完全正确的时间打电话,其实Asp.Net调用GC。收集在某些点类似于你所描述的。

在GC是聪明的有关调用GC.Collect的,如果你叫GC.Collect的,气相色谱可以覆盖你的决定,仍然没有收集(你有,当你调用GC.Collect选择设置标志这种行为),这是在调用GC.Collect的推荐的方式,因为你还是让GC决定,如果它是一个很好的时间来收集。

不要把我的建议是调用GC.Collect的一般性发言,你应该总是避免调用它,除非你真的确定你需要调用它,就像你描述的情况也正是为什么GC。收集在那里。

你会得到称其正在迅速释放生活垃圾的好处,通常你会在乎这种情况下,如果

您在内存不足的情况​​,并希望急于有关集合,如果你是在内存不足的情况​​下,GC将是积极不管怎样,就会自动踢如果内存pressure高机 如果你想,以避免在内存不足的情况​​下,你要收集热切。

希望这有助于。 谢谢

Ok, I've read a couple of topics about it, but here it goes. Let's imagine I have an application where basically every now and then I will click on a button, a lot of things will happen for a couple of minutes, and then it'll stay idle possible for another hour, or maybe just 1 minute. Wouldn't just after that whole ended a good situation to call GC.Collect? I mean, I do know that just at that moment I will not being using my application, and GC cannot guess it.

解决方案 Java性能优化全攻略

I can see that several people have gone extreme about not recommending to call GC.Collect.

GC.Collect is there for a reason, here are my recommendation of when and why to call GC.Collect.

In General, don't worry about calling it, GC tunes itself very well and will do the right thing.

Sometimes, you end up in situation where you know for sure that this is the right time to call it, the situation you described above is exactly the right time to call it, in fact Asp.Net calls GC.Collect at certain points that are similar to what you described.

The GC is smart about calling GC.Collect, if you called GC.Collect, the GC can override your decision and still doesn't collect ( you have to set a flag when you call GC.Collect to choose this behavior), this is the recommended way of calling GC.Collect, since you are still letting the GC decides if it is a good time to collect.

Don't take my recommendation is a general statement for calling GC.Collect, you should always avoid calling it, unless you REALLY sure you need to call it, situation like the one you described is exactly why GC.Collect is there.

The benefit you will get from calling it is freeing the garbage quickly, generally you will care about this situation if

You are in low memory situation and want to be eager about collection, if you are in low memory situation, the GC will be aggressive anyway and will kick in automatically if the memory pressure is high on the machine If you want to avoid getting in low memory situation and you want to collect eagerly.

Hope this helps. Thanks

 
精彩推荐
图片推荐