GC.Collect()

前端 未结 7 2007
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 19:21

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 thin

相关标签:
7条回答
  • 2020-11-27 19:57

    I'm hoping you're aware that calling GC.Collect doesn't cause more (or less) objects to be collected.

    If you're trying to optimize for time, do you know the time GC takes to collect objects in your application? On desktop operating systems (XP, Vista etc..), the CLR uses a concurrent GC, and it can run without suspending all threads in the application for the duration of the collection.

    Explicitly calling GC.Collect is not recommended because

    1. It throws the CLR GC Tuning algorithm out of gear. The tuner determines when to trigger a GC automatically, and forcing a manual GC messes with its calculations.

    2. By forcing a collection manually, you can end up promoting objects up a generation - objects which might have been collected in the next GC ( if they were "orphaned" before the GC decided to kick in).

    You might find it interesting that .NET 4.0, a GC notification mechanism has been introduced for these kinds of scenarios.

    0 讨论(0)
提交回复
热议问题