The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

 locked
[UWP] BitmapEncoder.SetPixelData Memory Leak RRS feed

  • Question

  • I am porting a Windows 8.1 Store photo app to a Windows 10 UWP app. I have a chroma key function that works perfectly under Windows 10 as a Windows 8.1 app. However, the same code in my new UWP version has a huge memory leak. Both are running in Visual Studio 2015 on Windows 10.

    Here is a code snippet:

    byte[] PicWithEffect = null;

    using(varimageStream = newInMemoryRandomAccessStream())

    {

    ImageEncodingPropertiesimageProperties = ImageEncodingProperties.CreateJpeg();

    awaitm_mediaCapture.CapturePhotoToStreamAsync(imageProperties, imageStream);

    awaitimageStream.FlushAsync();

    imageStream.Seek(0);

    PicWithEffect = chromaKeyEffect.ApplyEffect(awaitChromaKeyHelper.SelectPhoto(imageStream, m_nPreviewWidth, m_nPreviewHeight), chromaKeyEffect.BackgroundPreview);

    }

    using(varimageStream2 = newInMemoryRandomAccessStream())

    {

    BitmapEncoderencoder = awaitBitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, imageStream2);

    encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, m_nPreviewWidth, m_nPreviewHeight, 96, 96, PicWithEffect);

    awaitencoder.FlushAsync();

    WriteableBitmapbmp = newWriteableBitmap((int)m_nPreviewWidth, (int)m_nPreviewHeight);

    bmp.SetSource(imageStream2);

    imgChromaPreview.Source = bmp;

    }

    I have tracked down the source of the memory leak to the line that calls SetPixelData. If I comment out that line then, of course, my image is black, but it can run for hours with no memory increase. It seems like SetPixelData is keeping a reference to the byte[] PicWithEffect.

    Again, this is only in my UWP project. The Windows 8.1 project does not leak with exactly the same code.

    Please help!

    Thank you,


    Thaine Norris


    • Edited by Fred Bao Monday, September 21, 2015 7:58 AM add the tag
    Friday, September 18, 2015 3:35 AM

Answers

  • Hi Thaine,

    I wanted to give you a quick status update. The team that owns the BitmapEncoder class and the SetPixelData method investigated your report. They confirmed the behavior you reported, but at the moment all signs point to the issue actually existing on the Visual Studio debugger itself, not the BitmapEncoder.

    We are involving the Visual Studio team in the investigation, and hopefully we'll get to the bottom of this soon.

    Thanks again!

    • Marked as answer by dtnorris Tuesday, October 13, 2015 4:14 PM
    Wednesday, October 7, 2015 9:57 PM

All replies

  • Hello Thaine Norris,

    >>I have tracked down the source of the memory leak to the line that calls SetPixelData. If I…

    I would also recommend you could follow this blog - Troubleshooting Memory Leaks in Windows Store Apps to use the NP .NET Profiler to detect which object causes the memory leak.

    >>Again, this is only in my UWP project. The Windows 8.1 project does not leak with exactly the same code.

    Could you please share us a repro project? We could help test it to see if we could reproduce it on our side, if the scenario occurs, we would open an internal discussion to check if this is related with the new windows 10.

    PS: After making a test, the profiler tool seems to not work in windows 10. So just ignore it.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Fred Bao Monday, September 21, 2015 8:40 AM
    Monday, September 21, 2015 7:58 AM
  • Hi Fred,

    I will put together two sample projects today.

    Thank you!


    Thaine Norris

    Monday, September 21, 2015 4:14 PM
  • Hi Fred,

    I have two projects based on the new CameraStarterKit sample. One is a Windows 8.1 project and the other is a Windows 10 UWP project. The memory leak is clearly visible on the Windows 10 version.

    How do I get these projects to you?

    Best regards,


    Thaine Norris

    Monday, September 21, 2015 5:54 PM
  • Hello?

    How do I get the test projects to you? Please advise.

    Thank you,


    Thaine Norris

    Tuesday, September 22, 2015 3:24 AM
  • Hello dtnorris,

    You could use the onedrive.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 22, 2015 12:31 PM
  • Hello Fred,

    Here is a link to the two ZIP files containing 8.1 and UWP projects.

    https://onedrive.live.com/redir?resid=420D4241D4D3848B!179827&authkey=!AGBaY725GcsglHY&ithint=folder%2c

    Please note that I could have done the chroma key effect with the AddEffectAsync API on the MediaCapture object. However, since we also support Sony WiFi-connected cameras, we must apply the effect to the JPG image stream in real time. That is why I made these webcam sample projects use the code snippet above with the call to SetPixelData, so that they would use the same method as the Sony code. The memory leak was originally discovered in the Sony code. AddEffectAsync does not cause any memory issues. 

    Thank you for looking into this,


    Thaine Norris


    • Edited by dtnorris Tuesday, September 22, 2015 4:11 PM
    Tuesday, September 22, 2015 4:08 PM
  • Hello again,

    What is the status of this?

    Thank you,


    Thaine Norris

    Thursday, September 24, 2015 2:45 AM
  • Hello dtnorris,

    I would download you provided sample to check if I could reproduce this scenario on my side, if it is the same result, I would open an internal discussion, and it may take some time and as soon as we get any result, I will post an update to here.

    Thanks for your understanding.

    Best Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, September 24, 2015 10:36 AM
  • Hello Fred,

    Were you able to duplicate the issue?

    To be able to use SetPixelData is a mission critical feature for two of Windows Store our apps. We're stuck at the moment.

    Thank you!


    Thaine Norris



    • Edited by dtnorris Monday, September 28, 2015 6:00 PM
    Monday, September 28, 2015 6:00 PM
  • Hello dtnorris,

    I downloaded your two samples, and both run them for about five minitues. What I see is that the windows 10s app memory is about 145MB and has not grouped up too much. So I would think if this is an environment issue, do you have a try to run your app on a different device?

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, September 29, 2015 8:45 AM
  • Hi Fred,

    Thank you for testing it! Did you run it inside the debugger?

    I did another test this morning and found good news and bad news. The good news is that if I run the app outside the debugger, it doesn't appear to leak! I ran it for about 10 minutes with no discernable increase in process memory. This is a huge relief.

    However, running it inside the debugger caused it to eat up nearly 700 MB of memory in just two minutes. See the following screen capture showing the Windows Task Manager next to the VS 2015 debugger.

    Scren capture of Task Manager and VS 2015 debugger

    So I guess this is good news over all. Hopefully this issue can be fixed in the VS 2015 tools.

    Thanks again,


    Thaine Norris

    Tuesday, September 29, 2015 3:11 PM
  • Hi Thaine,

    Thanks so much for reporting the issue and providing a way for us to reproduce the issue.

    I have tried them on my machine and they appear to behave just as you described:

    • Running without a debugger attached does not appear to leak memory
    • Running under the debugger causes a memory leak
    • Commenting out the call to SetPixelData appears to fix the issue

    I will attempt to find the team that owns this component and follow up with them. I'm keeping this thread bookmarked, and I'll update you as soon as I know more.

    Tuesday, September 29, 2015 7:23 PM
  • Wonderful!

    Thaine Norris

    Tuesday, September 29, 2015 7:29 PM
  • Hi Thaine,

    I wanted to give you a quick status update. The team that owns the BitmapEncoder class and the SetPixelData method investigated your report. They confirmed the behavior you reported, but at the moment all signs point to the issue actually existing on the Visual Studio debugger itself, not the BitmapEncoder.

    We are involving the Visual Studio team in the investigation, and hopefully we'll get to the bottom of this soon.

    Thanks again!

    • Marked as answer by dtnorris Tuesday, October 13, 2015 4:14 PM
    Wednesday, October 7, 2015 9:57 PM
  • Thank you for digging into it! It is a relief that it appears to be a Visual Studio debugger issue and not a runtime issue.

    Best regards,


    Thaine Norris

    Tuesday, October 13, 2015 4:14 PM
  • Hi. I know this thread is kind of old, but I have the same/similar problem with an UWP app I am working on. Are you aware of the status of this fix (I use VS 2017).
    Tuesday, March 20, 2018 11:59 PM