I am using this function which convert thy byte array to image but when this function calls the memory usage of system increasing.This function can be called around 500 times.I tried to dispose or flush to make memory empty but usage still getting increases.I am attaching a task manager image which is showing memory usage.
public static BitmapImage ConvertToBitmapImage(byte[] imageData)
{
if (imageData == null || imageData.Length == 0) return null;
var image = new BitmapImage();
using (var mem = new MemoryStream(imageData))
{
mem.Position = 0;
image.BeginInit();
image.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = null;
image.StreamSource = mem;
image.EndInit();
mem.Flush();
mem.Dispose();
}
image.Freeze();
return image;
}
Don't do this:
using (var mem = new MemoryStream(imageData))
{
...
mem.Dispose();
}
You are already closing mem
with using
. Also mem.Position
should be 0
by default. But that's not your problem.
Try Adding a Background Thread
and check the process memory usage on a while
loop, if it exceeds x
then call GC.Collect()
, so this Disposed elements get deallocated . Also after you're done doing whatever with the BitmapImage
set it to null just in case.
image.CacheOption = BitmapCacheOption.OnLoad;
Maybe this line has something to do with memory usage, does BitmapCacheOption.Default
make any difference?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.