C# MODI内存泄漏
我有一个应用程序,我正在使用MODI 2007对几个多页tiff文件进行OCR。我发现,当我在一个目录中启动它时,该目录包含几个好的TIFF,但也包含一些无法在Windows Picture and Fax Viewer中打开的TIFF,那么MODI也无法OCR这些“坏”TIFF。发生这种情况时,应用程序无法回收MODI用于OCR这些TIFF的任何内存。在该工具尝试OCR太多这些“坏”TIFF后,机器内存不足,应用程序崩溃。我曾尝试过几次网络代码修复,据称修复了任何MODI内存泄漏,但迄今为止没有一次对我有效。我正在粘贴下面进行OCR的代码部分:C# MODI内存泄漏,c#,memory-leaks,ocr,modi,C#,Memory Leaks,Ocr,Modi,我有一个应用程序,我正在使用MODI 2007对几个多页tiff文件进行OCR。我发现,当我在一个目录中启动它时,该目录包含几个好的TIFF,但也包含一些无法在Windows Picture and Fax Viewer中打开的TIFF,那么MODI也无法OCR这些“坏”TIFF。发生这种情况时,应用程序无法回收MODI用于OCR这些TIFF的任何内存。在该工具尝试OCR太多这些“坏”TIFF后,机器内存不足,应用程序崩溃。我曾尝试过几次网络代码修复,据称修复了任何MODI内存泄漏,但迄今为止没
StringBuilder strRecText = new StringBuilder(10000);
MODI.Document doc1 = new MODI.Document();
doc1.Create(name);
try
{
doc1.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true); // this will ocr all pages of a multi-page tiff file
}
catch (Exception e)
{
doc1.Close(false); // clean up
if (doc1 != null)
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(doc1);
doc1 = null;
}
}
MODI.Images images = doc1.Images;
for (int imageCounter = 0; imageCounter < images.Count; imageCounter++)
{
if (imageCounter > 0)
{
if (!noPageBreakFlag)
{
strRecText.Append((char)pageBreakChar);
}
}
MODI.Image image = (MODI.Image)images[imageCounter];
MODI.Layout layout = image.Layout;
strRecText.Append(layout.Text);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
if (layout != null)
{
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(layout);
layout = null;
}
if (image != null)
{
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(image);
image = null;
}
}
File.AppendAllText(ocrFile, strRecText.ToString()); // write the OCR file out to disk
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
if (images != null)
{
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(images);
images = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
doc1.Close(false); // clean up
if (doc1 != null)
{
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(doc1);
doc1 = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
StringBuilder strRecText=新的StringBuilder(10000);
MODI.Document doc1=新的MODI.Document();
doc1.创建(名称);
尝试
{
doc1.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH,true,true);//这将OCR多页tiff文件的所有页面
}
捕获(例外e)
{
doc1.关闭(错误);//清理
如果(doc1!=null)
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
System.Runtime.InteropServices.Marshal.FinalEleaseComObject(doc1);
doc1=null;
}
}
MODI.Images=doc1.Images;
对于(int-imageCounter=0;imageCounter0)
{
如果(!noPageBreakFlag)
{
追加((char)pageBreakChar);
}
}
MODI.Image=(MODI.Image)图像[图像计数器];
MODI.Layout=image.Layout;
strRecText.Append(layout.Text);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
if(布局!=null)
{
System.Runtime.InteropServices.Marshal.FinalEleaseComObject(布局);
布局=空;
}
如果(图像!=null)
{
System.Runtime.InteropServices.Marshal.FinalEleaseComObject(图像);
image=null;
}
}
File.AppendAllText(ocrFile,strRecText.ToString());//将OCR文件写入磁盘
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
如果(图像!=null)
{
System.Runtime.InteropServices.Marshal.FinalEleaseComObject(图像);
图像=空;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
doc1.关闭(错误);//清理
如果(doc1!=null)
{
System.Runtime.InteropServices.Marshal.FinalEleaseComObject(doc1);
doc1=null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
在过去的几个月里,我一直在使用MODI进行一个项目。到目前为止,MODI是我尝试过的最精确的OCR引擎,但它在释放资源和崩溃方面存在一些重大问题
我最终构建了一个命令行应用程序,它将图像的路径作为命令行参数,然后将生成的文本保存到文件并退出。然后,任何需要modi功能的软件都可以使用该命令行应用程序。这听起来很奇怪,但它是解决MODI内存泄漏问题的一种非常简单直接的方法,因为当命令行进程存在时,它的内存由操作系统释放,因此您不必担心应用程序崩溃或资源未被清理。我发现,与实际OCR图像所需的时间相比,启动命令行exe然后读取它创建的文件所需的时间是非常微不足道的,因此您实际上并没有在性能方面损失太多