[UWP]BitmapEncoder memory leak RRS feed

  • Question

  • environment: UWP 1.1, vs2015

    code snippet:

                    using (var ms = new InMemoryRandomAccessStream())
                        var bitmapEncoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, ms);
                        bitmapEncoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Ignore,
                           (uint)width, (uint)height,
                           96, 96, frameBuf);
                        await bitmapEncoder.FlushAsync();
                        byte[] bytes = new byte[ms.Size];
                        using (var s = ms.AsStream())
                            await s.ReadAsync(bytes, 0, bytes.Length);
                        //await ms.AsStream().ReadAsync(bytes, 0, bytes.Length);
                        //other stuff with the output bytes...
                        frameBuf = null;
                        bitmapEncoder = null;
                        bytes = null;
                    //GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);

    frameBuf is the pixels data which are in corrected format as the image generating could be displayed correctly.


    above code are being called frequently, the generated JPEG will be displayed in an Image control (I can't use MediaElement as I need to custom a lot of stuff).

    Memory keep going up, within a minute OutOfMemory exception will be thrown. Even if I remove the remark of last line to force the GC, memory didn't get released at all.

    memory leak won't happen if I comment this line: //await bitmapEncoder.FlushAsync(); but of course there is no image bytes will be generated.

    Anyone has any clue how to fix it? I had tried a lot of workaround, such as changing different encoder (PNG, BMP), no luck.

    IP CAM Controller

    Sunday, October 4, 2015 1:56 PM

All replies

  • Could you use the Lumia Imaging sdk instead?


    Sunday, October 4, 2015 3:23 PM
  • I done some reading, it looks like JpegRender should be the one I need? In my case, I have a lot of video frames while each frame had been decoded as raw bytes in RGBA8 format (it could be others such as Nv12 but RGBA8 is the one I tested most compatible with windows), and I need to convert the bytes into a JPEG image that eventually Image control is able to display.

    would you mind point me to some concrete sample or article how I should make use of the SDK? Thanks!

    IP CAM Controller

    Monday, October 5, 2015 8:14 AM
  • You should set the Size property on the renderer. That will resize the image to the size you want.

    Looking at the JpegRenderer (https://msdn.microsoft.com/en-us/library/lumia.imaging.jpegrenderer_members.aspx) set the Size to what you want the size to be. In addition you can set the OutputOption property (https://msdn.microsoft.com/en-us/library/lumia.imaging.outputoption.aspx) if you want the contents to be Stretched or to preserve aspect ratio instead.

    A quick example:

    using (var source = ...)
    using (var renderer = new JpegRenderer(source))
       renderer.Size = new Size(800, 600);
       renderer.OutputOption = OutputOption.Stretch;
       var result = await renderer.RenderAsync();

    If you are using a BitmapRenderer or a WriteableBitmapRenderer and pass in the (writeable)bitmap the renderer will automatically resize the contents to the size of that image.

    Wednesday, October 21, 2015 8:37 AM
  • Thanks Mortene! Eventually I translate the "same code" from c# to c++, put it in a library and call from c#, then the memory leak issue is resolved by itself.

    I have done a few tricks like that under UWP development, all codes are exactly the same except that language syntax(c# vs c++). So if anyone encounter a memory leaking issue might consider putting the leaking method into c++. I suspect that the framework (in .net level) has some sort of bugs that causing this while c++ seems to has more control on assigning and releasing memory and so if you explicitly release an object, it's guaranteed to be cleared, but just my guess. 

    IP CAM Controller

    Friday, December 4, 2015 1:49 AM