Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF BitmapImage Doesn't Dispose

I have loaded a list of images out of a filestream database. When I load them initially the memory jump isn't that large. When I display them on the screen my memory usage spikes and doesn't go away even after I dispose of the BitmapImage streamsource and set it to Nothing.

    Dim newItem As New MIdentifiedImage
    Dim data As Byte() = dt.Rows(i).Item("ScannedImage")
    Dim strm As New MemoryStream(data)

    Dim bi As New BitmapImage()
    bi.BeginInit()
    bi.StreamSource = strm
    bi.EndInit()
    bi.Freeze()
    newItem.ScannedImage = bi

And here is my dispose code

  For Each img In InvoiceObj.ImageList
    img.ScannedImage.StreamSource.Dispose()
    img.ScannedImage.StreamSource = Nothing

  Next

So my profiler says that the memory usage is low but when I check in my task manager the memory usage is quite high. If I continue to load images beyond 1.5GB of memory it just stops showing the images on screen.

like image 616
Jonah Kunz Avatar asked Apr 19 '26 20:04

Jonah Kunz


1 Answers

You need to clear out the ImageList, as well. Just setting the StreamSource to null doesn't cause it to remove the data that's already loaded. By clearing the ImageList when you're done with it, you allow the GC to clean up the actual BitmapImage instances, as well.

like image 138
Reed Copsey Avatar answered Apr 21 '26 10:04

Reed Copsey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!