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.
WPF BitmapImage Doesn't Dispose
-
30-06-2022 - |
Question
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.
La solution
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow