Best Practice for Forcing Garbage Collection in C#

前端 未结 15 1956
天命终不由人
天命终不由人 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:37

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

    IMHO, this is similar to saying "If you can prove that your program will never have any bugs in the future, then go ahead..."

    In all seriousness, forcing the GC is useful for debugging/testing purposes. If you feel like you need to do it at any other times, then either you are mistaken, or your program has been built wrong. Either way, the solution is not forcing the GC...

提交回复
热议问题