Best Practice for Forcing Garbage Collection in C#

前端 未结 15 2048
天命终不由人
天命终不由人 2020-11-22 12:28

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\

15条回答
  •  我在风中等你
    2020-11-22 12:53

    One more thing, triggering GC Collect explicitly may NOT improve your program's performance. It is quite possible to make it worse.

    The .NET GC is well designed and tuned to be adaptive, which means it can adjust GC0/1/2 threshold according to the "habit" of your program memory usage. So, it will be adapted to your program after some time running. Once you invoke GC.Collect explicitly, the thresholds will be reset! And the .NET has to spent time to adapt to your program's "habit" again.

    My suggestion is always trust .NET GC. Any memory problem surfaces, check ".NET Memory" performance counter and diagnose my own code.

提交回复
热议问题