【问题标题】:Releasing Memory from unused Bitmapsources从未使用的位图源中释放内存
【发布时间】:2016-01-28 04:25:47
【问题描述】:

我尝试通过以下代码将 .RAW 文件中的大图像加载到 System.Windows.Controls.Image 中:

Dim pf As PixelFormat = PixelFormats.Gray16
Dim rawStride As Integer = Convert.ToInt32((Convert.ToInt32(RAWImage.ImageSize.Width) * pf.BitsPerPixel + 7) \ 8)
bitmap = BitmapSource.Create(Convert.ToInt32(RAWImage.ImageSize.Width), Convert.ToInt32(RAWImage.ImageSize.Width), 96, 96, _
                                                         pf, Nothing, ImagePixelvalues, rawStride)
MainPictureBox.Source = bitmap

其中 ImagePixelvalues 是 uint16()。 我的问题是由bitmapsource 引起的内存泄漏。例如,如果我在图像控制中加载 100 倍 RAW 图像,Windows 任务管理器性能显示系统的物理内存会在一段时间后增加。这意味着以前的bitmapsource 没有被释放,GC 不能收集它们。请让我知道您对此的想法如何发布bitmapsource

【问题讨论】:

  • 你怎么知道BitmapSource负责增加内存消耗?它也可能是您的 ImagePixelvalues。您应该考虑使用内存分析器。
  • 你是如何发布图像的?您是在尝试强制 GC,还是尝试了任何技巧(例如:setprocessworkingsetsize)?
  • @Clemens:我将 ImagePixelValue 设为 null,但问题仍然存在。
  • @Caveman:我使用了 GC.Collection() 但没有发生任何事情我对 setprocessworkingsetsize 没有任何影响!!!

标签: wpf vb.net image-processing memory-leaks


【解决方案1】:

当你用完每个位图后,你可以试试

   bitmap.Dispose

【讨论】:

  • 亲爱的大卫位图类没有 Dispose 方法,这是我的问题,我无法处理位图:D
  • 我的意思是你定义的位图变量
  • 我使用 WPF 的 bitmapsource,我对您的解决方案感到困惑我没有定义新的位图类
  • 这一行.. MainPictureBox.Source = bitmap.. 位图不是变量吗?
  • 是的,当然:Dim bitmap As BitmapSource = BitmapSource.Create(Convert.ToInt32(ImageSize.Width), Convert.ToInt32(ImageSize.Height), 96, 96, _ pf, Nothing, PixelValues, rawStride)
猜你喜欢
  • 2017-09-22
  • 2013-09-21
  • 2019-01-20
  • 2011-05-05
  • 1970-01-01
  • 2018-11-08
  • 2015-06-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多