在 for...next 循环中如何处理位图和图像?
特别是
image(of bgr, byte)
图像。
“image(of bgr, byte)”图像来自 emgu 库。
当您处理单个位图或图像时,
Dispose()
命令可以正常工作。
但是当您循环访问多个图像时,会出现内存不足错误。
即使应用
using ... end using
命令,也会出现内存问题。
例如,这会给我抛出内存错误:
Dim pic As Bitmap = New Bitmap(270, 100)
For i = 0 to 100
Dim CleanImage As New Image(Of Bgr, Byte)(pic)
'this command comes from the emgu library - tesseract
OCRz.Recognize(CleanImage)
CleanImage.Dispose()
Next
一如既往:
Dim pic As Bitmap = New Bitmap(270, 100)
For i = 0 to 100
Using CleanImage As New Image(Of Bgr, Byte)(pic)
OCRz.Recognize(CleanImage)
End Using
Next
我已经尝试过
GC.Collect()
。这不起作用。
如何正确清除程序中这些烦人的位图和图像?
我迫切需要解决方案。
emgu wiki 声明如下:
自动垃圾收集 Image 类 自动处理内存管理和垃圾 收藏。
一旦垃圾收集器决定不再有引用 Image 对象,它将调用 Dispose 方法, 它释放非托管 IplImage 结构。
垃圾收集器决定处理图像的时间不是 保证。当处理大图像时,建议调用 Dispose() 方法显式释放对象。或者,使用 C# 中的 using 关键字限制图像的范围
using (Image<Gray, Single> image = new Image<Gray, Single>(1000, 800)) { ... //do something here in the image } //The image will be disposed here and memory freed
这正是我正在做的事情。然而,我收到了这个 OutOfMemory 异常。这是怎么回事?
2023 年之前都会出现同样的错误!!!!!!!