When is it acceptable to call GC.Collect?

前端 未结 24 2582
挽巷
挽巷 2020-11-22 08:54

The general advise is that you should not call GC.Collect from your code, but what are the exceptions to this rule?

I can only think of a few very speci

24条回答
  •  生来不讨喜
    2020-11-22 09:15

    using(var stream = new MemoryStream())
    {
       bitmap.Save(stream, ImageFormat.Png);
       techObject.Last().Image = Image.FromStream(stream);
       bitmap.Dispose();
    
       // Without this code, I had an OutOfMemory exception.
       GC.Collect();
       GC.WaitForPendingFinalizers();
       //
    }
    

提交回复
热议问题