I'm creating a streaming app that get byte from server continuity with this code:
while (true)
{
byte[] buffer = await _client.GetStreamOneCameraAsync();
if (buffer.Length == 0) continue;
SoftwareBitmap softwareBitmap = SoftwareBitmap.CreateCopyFromBuffer(buffer.AsBuffer(), BitmapPixelFormat.Gray8, 1280, 960);
//buffer = null; if I put this here, it will solve the memory leak
//GC.Collect();
if (softwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 ||
softwareBitmap.BitmapAlphaMode == BitmapAlphaMode.Straight)
{
softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
}
_imageSource = new SoftwareBitmapSource();
await _imageSource.SetBitmapAsync(softwareBitmap);
// Set the source of the Image control
image_Preview.Source = _imageSource;
}
So my question is: Is it enough to clear all the memory that byte[] taken? My next task is display that image to the UI. So maybe it will make the memory leak?
UPDATE: Thanks you guys for supporting. I just realize that my memory leak problem was came out when I convert that buffer to image and display it into the UI and not dispose it. it doesn't due to the byte[]
UPDATE 2: I updated full of my code. Please help me to check where make the memory leak?