Garbage collection fails to reclaim BitmapImage?

前端 未结 5 1383
被撕碎了的回忆
被撕碎了的回忆 2020-12-11 05:10

I have an application(WPF) which creates BitmapImages in huge numbers(like 25000). Seems like framework uses some internal logic so after creation there are approx 300 mb of

5条回答
  •  庸人自扰
    2020-12-11 05:53

    I followed the answer given by AAAA. Orignal code causing memory filled up is:

    if (overlay != null) overlay.Dispose();
    overlay = new Bitmap(backDrop);
    Graphics g = Graphics.FromImage(overlay);
    

    Inserted AAAA's code block, C# add "using System.Threading;" and VB add "Imports System.Threading":

    if (overlay != null) overlay.Dispose();
    //--------------------------------------------- code given by AAAA
    Thread t = new Thread(new ThreadStart(delegate
    {
        Thread.Sleep(500);
        GC.Collect();
    }));
    t.Start();
    //-------------------------------------------- \code given by AAAA
    overlay = new Bitmap(backDrop);
    Graphics g = Graphics.FromImage(overlay);
    

    Repeat looping this block now makes a steady and low memory footprint. This code worked using Visual Studio 2015 Community.

提交回复
热议问题