Best Practice for Forcing Garbage Collection in C#

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

    I think you already listed the best practice and that is NOT to use it unless REALLY necessary. I would strongly recommend looking at your code in more detail, using profiling tools potentially if needed to answer these questions first.

    1. Do you have something in your code that is declaring items at a larger scope than needed
    2. Is the memory usage really too high
    3. Compare performance before and after using GC.Collect() to see if it really helps.

提交回复
热议问题