WPF not releasing memory on certain computers <p>On certain computers I have a WPF application that doesn't release memory when working with it. For instance when sending a document to the printer, on most computers it's releasing memory all the 2-3 seconds (it's going up for maybe 200 megs then coming back down), which is normal behaviour and when the printing is done I go back to my initial memory state.</p> <p>But on some computers (over 20 computers installed and only one is giving me this issue) it's not freeing the memory. It keeps piling up. I don't mind seeing memory going to 1.5 Gb as long as it's releasing it in the end, but it's not and I get an OutOfMemoryException.</p> <p>I don't have full access to the problematic computer (they're a client's computer we installed a week ago and we just saw this problem) so I can't really test it but it's a standard Windows 7 Pro x64, with 10Gb of RAM and aside from that, it's working like a charm.</p> <p>Also it's not ONLY when printing. The application is kind of a PDF viewer and everytime a new page is loaded for the user, the previous page is freed from memory. Again, it's working fine on most PC, but not in these case.</p> <p>Is there anything that could prevent the memory from being released? I can't seem to find a similar problem anywhere on the web.</p> <p>EDIT: Okay I got a hold of the computer for an hour. I was able to test two things :</p> <ol> <li>GC.Collect() didn't arrange anything (I even forced it with GC.WaitForPendingFinalizer)</li> <li>I tried disposing of the DocumentPage in my Paginator, no luck. I also kept a reference of a ViewModel I was using to display my page on printing, I tried disposing it : didn't work.</li> </ol> <p>What I can say is that in both cases it must be because of the images displayed in my pages. Here's the function I call to get a new page image :</p> <pre><code>'First I get the path to the images Dim path As String = String.Format("{0}\{1}.png", Me.TemporaryFolderPath, page.PageId) Dim imgSource As CachedBitmap 'If the file doesn't exist If Not IO.File.Exists(path) Then 'A function is called which creates the png file for next uses (this way the first loading is slow, but the next times it's faster) imgSource = Pdf.GetPageImage(page.PageNumber, path) Else 'If the file exists I instantiate a new BitmapImage Dim img As New BitmapImage 'And I load it in a stream Using stream As IO.FileStream = IO.File.OpenRead(path) 'I apply the stream to my image img.BeginInit() img.CacheOption = BitmapCacheOption.OnLoad img.StreamSource = stream img.EndInit() 'Flush, close, dispose of my stream stream.Flush() stream.Close() End Using 'And I create a CachedBitmap with this image (which is almost like an ImageSource) imgSource = New CachedBitmap(img, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad) img = Nothing End If 'If my ImageSource is something, I freeze it so that the memory is freed afterwards If imgSource IsNot Nothing Then imgSource.Freeze() Return imgSource </code></pre> <p>All this (freezing the image, setting the cacheOption to OnLoad, loading from a stream) I did to avoid memory leaks. My first attempt to load an image has a huge leak and I refactored my function so that I didn't have this problem anymore. </p> <p>Is there anything there that could be the problem?</p>