我的应用程序具有从数据库导出数据的功能。它使用 Ionic ZIP 库构建一个包含数据的 zip 文件。
该过程通过检索每页 1,000 行的数据来工作。检索到的数据每行数据包含 2 个图像 (JPEGS)。“导出”包含一个 HTML 文件,其中包含一个 HTML 表格。该表包含来自每一行的重要信息和图像的链接。每个图像都作为 JPEG 文件存储在 ZIP 文件中。编写的 HTML 包含一个链接到相应图像文件的锚标记。
一切正常,但我最近发现OutOfMemoryException
在导出大量行时会抛出 an (在我发现问题的情况下超过 24,000 行)。在查看我的代码时,我没有看到任何明显的泄漏(或者我不会发布这个,对吗?;))
这是输出正在导出的数据的方法:
private void OutputReads( StreamWriter writer, ZipFile zipFile, BackgroundWorker worker, ExportArgs args ) {
int ReadCount = Math.Min( args.ReadMatches, args.ResultsLimit );
writer.WriteLine( "<h2>Matching Reads:</h2>" );
writer.WriteLine( "<p>Number in Database: {0}</p>", args.ReadMatches.ToString( "#,##0" ) );
writer.WriteLine( "<p>Number in Report: {0}</p>", ReadCount .ToString( "#,##0" ) );
writer.WriteLine();
if ( args.ReadMatches == 0 ) {
return;
}
writer.WriteLine( "<table border=\"1\" cellspacing=\"0\" bordercolordark=\"black\" bordercolorlight=\"black\">" );
writer.WriteLine( "<tr>" );
writer.WriteLine( "<th width=\"110\"><b>Plate</b></th>" );
writer.WriteLine( "<th width=\"60\"><b>State</b></th>" );
writer.WriteLine( "<th width=\"200\"><b>Date / Time</b></th>" );
writer.WriteLine( "<th width=\"142\"><b>Latitude</b?</th> " );
writer.WriteLine( "<th width=\"142\"><b>Longitude</b?</th> " );
writer.WriteLine( "<th width=\"125\"><b>Alarm Class</b></th>" );
writer.WriteLine( "<th width=\"150\"><b>Notes</b></th>" );
writer.WriteLine( "<th width=\"150\"><b>Officer Notes</b></th>" );
writer.WriteLine( "<th width=\"150\"><b>Images</b></th>" );
writer.WriteLine( "</tr>" );
int noPages = ReadCount / args.PageSize + ( ReadCount % args.PageSize == 0 ? 0 : 1 );
for ( int pageNo = 0, count = 0; pageNo < noPages; pageNo++ ) {
if ( worker.CancellationPending ) {
return;
}
ReadViewModel[] reads = null;
try {
reads = DataInterface.GetReads( args.LocaleCode, args.Plate,
args.StartDate, args.EndDate,
args.AlarmClasses, args.HotListId,
args.PageSize, pageNo
);
} catch ( DataAccessException ex ) {
. . .
} catch ( ThreadAbortException ) {
// The thread is stopping. Stop processing now.
} catch ( Exception ex ) {
. . .
}
foreach ( ReadViewModel read in reads ) {
if ( worker.CancellationPending ) {
return;
}
writer.WriteLine( "<tr>" );
writer.WriteLine( "<td width=\"110\"><p align=\"left\" style=\"text-align:left\">{0}</p></td>" , read.Plate );
writer.WriteLine( "<td width=\"60\" ><p align=\"center\" style=\"text-align:center\">{0}</p></td>", read.State );
writer.WriteLine( "<td width=\"150\"><p align=\"center\" style=\"text-align:center\">{0}</p></td>", read.TimeStamp );
writer.WriteLine( "<td width=\"125\"><p align=\"center\" style=\"text-align:center\">{0}</p></td>", read.GPSInformation == null ? " " : read.GPSInformation.Position.Latitude.ToString( "##0.000000" ) );
writer.WriteLine( "<td width=\"125\"><p align=\"center\" style=\"text-align:center\">{0}</p></td>", read.GPSInformation == null ? " " : read.GPSInformation.Position.Longitude.ToString( "##0.000000" ) );
writer.WriteLine( "<td width=\"125\"><p align=\"center\" style=\"text-align:center\">{0}</p></td>", read.AlarmClass == null ? " " : read.AlarmClass );
writer.WriteLine( "<td width=\"150\"><p align=\"left\" style=\"text-align:left\">{0}</p></td>" , read. Notes == null ? " " : read. Notes );
writer.WriteLine( "<td width=\"150\"><p align=\"left\" style=\"text-align:left\">{0}</p></td>" , read.OfficerNotes == null ? " " : read.OfficerNotes );
if ( read.ImageData == null ) {
try {
DataInterface.GetPlateImage( read );
} catch ( DataAccessException ex ) {
. . .
} catch ( Exception ex ) {
. . .
}
}
if ( read.OverviewImages == null ) {
try {
DataInterface.GetOverviewImages( read );
} catch ( DataAccessException ex ) {
. . .
} catch ( Exception ex ) {
. . .
}
}
if ( read.ImageData != null ) {
string ext = LPRCore.CarSystem.ImageDataAccessor.GetImageFileExtension( read.ImageData );
writer.Write( "<td width=\"150\"><p align=\"left\" style=\"text-align:left\"><a href=\".\\Images\\{0}.{1}\" target=\"_blank\">BW</a>", read.ID.ToString( "N" ), ext );
string fileName = string.Format( ".\\Images\\{0}{1}", read.ID.ToString( "N" ), ext );
if ( !zipFile.ContainsEntry( fileName ) ) {
zipFile.AddEntry( fileName, read.ImageData );
}
} else {
writer.Write( "No Plate Image" );
}
if ( read.OverviewImages != null && read.OverviewImages.Length > 0 ) {
for ( int i = 0; i < read.OverviewImages.Length; i++ ) {
string ext = LPRCore.CarSystem.ImageDataAccessor.GetImageFileExtension( read.OverviewImages[ i ].ImageData );
writer.Write( " - <a href=\".\\Images\\{0}_C{1}{2}\" target=\"_blank\">Color {1}</a>", read.ID.ToString( "N" ), i == 0 ? string.Empty : i.ToString(), ext );
string fileName = string.Format( ".\\Images\\{0}_c{1}{2}", read.ID.ToString( "N" ), i == 0 ? string.Empty : i.ToString(), ext );
if ( !zipFile.ContainsEntry( fileName ) ) {
zipFile.AddEntry( fileName, read.OverviewImages[ i ].ImageData );
}
}
} else {
writer.Write( "No Overview Images" );
}
writer.WriteLine( "</p></td>" );
writer.WriteLine( "</tr>" );
count++;
worker.ReportProgress( count, args );
}
}
writer.WriteLine( "</table>" );
writer.WriteLine();
}
我在想也许 Zip 文件是问题所在,因为它没有被刷新到磁盘或类似的东西,并且随着行的处理而变得越来越大。
有没有一种方法可以将 zip 文件刷新到磁盘并释放所有图像,以便垃圾收集器释放它们,或者是否有另一种方法使用这个库来使用更少的内存构建 zip 文件?