WriteableBitmap Memory Leak?

前端 未结 5 1162
花落未央
花落未央 2020-12-30 01:48

i am using the code below to create a live tile, based on an UI element. It renders the uiElement on a WriteableBitmap, saves the bitmap + returns

5条回答
  •  醉酒成梦
    2020-12-30 01:55

    The Problem is that rectangle.RenderTransform is an instance of an object and if you set writableBitmap to null the rectangle.RenderTransform Object is still alive and holds the rectangle in the memory... so the solution is to edit the code as follows:

    private void CreateImage()
    {
       var rectangle = CreateRectangle();
       var writeableBitmap = new WriteableBitmap(rectangle, rectangle.RenderTransform);
    
       rectangle.RenderTransform = null; //and your memory would be happy ;)
       rectangle = null;
       writeableBitmap = null;
       GC.Collect();
    }
    

    see the memory screenshots...

    before:

    memory without setting rectangle.RenderTransform to null

    after:

    memory with setting rectangle.RenderTransform to null

提交回复
热议问题