2014-10-06 90 views
0

在某些计算机上,我有一个WPF应用程序在使用它时不释放内存。例如,在向打印机发送文档时,在大多数计算机上,它会在所有2-3秒内释放内存(它可能会增加200 megs然后再次下降),这是正常行为,打印完成后,我会返回到我的初始记忆状态。WPF在某些计算机上不释放内存

但是在某些电脑上(安装了20多台电脑,只有一台电脑给我这个问题),这并没有释放内存。它不断堆积。我不介意只要内存最终释放到1.5 Gb,但它不是,我得到一个OutOfMemoryException。

我无法完全访问有问题的计算机(它们是我们一周前安装的客户端计算机,我们只是看到了这个问题),所以我无法真正测试它,但它是标准的Windows 7 Pro x64 ,拥有10Gb的RAM,除此之外,它的功能就像一个魅力。

此外它不是唯一的打印。该应用程序是一种PDF查看器,每次为用户加载新页面时,前一页将从内存中释放。再说一次,它在大多数PC上运行良好,但在这种情况下不能。

有什么可以防止内存被释放?我似乎无法在网络上的任何地方找到类似的问题。

编辑:好吧,我抱了一个小时的电脑。我能够测试两件事情:

  1. GC.Collect的()没有安排任何东西(我甚至强迫它GC.WaitForPendingFinalizer)
  2. 我想在我的分页程序,没有运气的DocumentPage处置。我还保留了一个ViewModel的参考资料,用于在打印时显示我的页面,我试图处理它:没有工作。

我可以说的是,在这两种情况下,它必须是因为我的网页上显示的图像。下面是我打电话获得一个新的页面图像的功能:

'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 

这一切(冻结形象,cacheOption设置的OnLoad,从流加载)我做以避免内存泄漏。我第一次加载图像的尝试有一个巨大的泄漏,我重构了我的功能,所以我没有这个问题了。

有什么可能是问题吗?

+0

假设你试过强迫GC? – 2014-10-06 14:04:47

+0

我不得不承认......我没有。由于这是不好的做法,我不想强​​制GC.Collect()(这似乎是不必要的,因为它在大多数计算机上工作)。 – 2014-10-06 14:16:54

+0

我想不出另一种解决“OutOfMemoryExcpetion”的方法,或者通过发送更小的数据包。是否有可能只是“修补”那台特定的机器,如同它必须有*一些*潜在的问题? – 2014-10-06 14:21:56

回答

0

最后,我的问题是,我保持当前页面图像在ReadOnly属性。即使我正在处理图像(ReadOnly属性使用的局部变量),它也不会释放它。

也许是因为在我的ViewModels上实现了OnPropertyChanged,因为当我将它作为一个读/写属性,并且我在每次页面更改时将其设置为null,它就会在有问题的计算机上释放内存。

相关问题