0

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;
    }

Task Manager screen shot

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • 2
    Image is missing. Nevertheless. Task manager is not reliable. It shows the amount of memory the system has reserved. If your application has allocated a couple of large block, Windows may decide to pre-allocate some more memory for it. – GolezTrol Sep 05 '16 at 18:35
  • Sorry,i have edited the post you can see the image now.Application does'nt allocate the large block,it takes around 19mb until this function calls for getting the images. – Syed Kamran Ahmed Sep 05 '16 at 18:44
  • Also the memory usage has to increase as you are allocating a `new BitmapImage` (as I can't see the rest of your code I can't tell how long it is kept so I'm assuming you are not discarding it right away) - We also have no information about how large the image is you are passing in – UnholySheep Sep 05 '16 at 18:45
  • Image is 100x100 thumbnails,In my application i get the images in form of byte array from webservice,then all the images converts by using this function,every time this function calls for every instance of my class model e.g model has bytearray and bitmap image..Around 500 instance i get in my application. – Syed Kamran Ahmed Sep 05 '16 at 18:49
  • `void getImages(List models) { foreach(model in models) { model.Image=ConvertToBitmapImage (model.ByteArray); } }` – Syed Kamran Ahmed Sep 05 '16 at 18:54
  • It is probably the result of the images being created – Ňɏssa Pøngjǣrdenlarp Sep 05 '16 at 19:42
  • **Don't dispose the image twice. This is a very bad habit.** If you use `using` statement, then you should not call `Dispose` explicitly. – Phil1970 Sep 05 '16 at 20:08
  • Tried to remove the dispose methods still usage getting increase. – Syed Kamran Ahmed Sep 06 '16 at 05:11
  • Before you start fixing this, please read my initial comment again. Then, use [*proper* tools](http://stackoverflow.com/questions/14032515/how-to-get-the-amount-of-memory-used-by-an-application) to check the *actual* memory usage of your application. There might not be a leak at all. Again, Task Manager is *not* a reliable source of information about the actual memory use of your application. – GolezTrol Sep 06 '16 at 05:17

1 Answers1

0

Don't do this:

using (var mem = new MemoryStream(imageData))
{
   ...         
   mem.Dispose();
}

You are already closing mem with using. Also mem.Position should be 0by 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?

Pau C
  • 773
  • 4
  • 20