-2
我正尝试动态生成大量(> 5000)的pdf文档。问题是在pdf生成过程中,我收到了内存不足异常。我决定将我的5000页PDF分成更小的500页pdf,但是当我尝试这种方式时.net仍然使用相同的内存。有没有办法强制我的应用程序释放一个变量仍在使用的未使用内存?这里是导致问题的代码如何强制.net以释放内存?
For Each patronRow As DataRow In patronsDatatable.Rows
.....some other code
If FARPrintOneLetterPerStudent = False Then
If farApplicationID <> _previousFarApplicationID Or _firstapp = True Then
lettercount = lettercount + 1
'************* test code *****************
If lettercount = 500 Then
If Me._printLabels = False Then
FixEndPdf(finalpdf)
End If
' Export moved from dataprovider
smallpdfcount = smallpdfcount + 1
ExportToPdf(ImportRtf(finalpdf), smallpdfcount)
finalpdf = Nothing
_isfirst = True
lettercount = 0
Else
If lettercount < 500 Then ' we only need to add to this variable when there is less than 500 pages
finalpdf = AddtoletterPdf(letterBody, printmultiple)
'_previewpdf = finalpdf
End If
_previousFarApplicationID = farApplicationID
_firstapp = False
End If
End If
Else
finalpdf = AddtoletterPdf(letterBody, printmultiple)
End If
'create a record in LettersDatatable for POS and FAR letters
AddToLettersDataTable(letterBody, patronID, patronName, farApplicationID, headerImageBuffer)
Else
'Create a record in labels datatable
AddToLabelsDatatable(letterBody, patronName, patronID, farApplicationID)
End If
End If
Next
Public Function ImportRtf(ByVal content As String) As RadDocument
Dim provider As New RtfFormatProvider()
Try
Return provider.Import(content)
Catch ex As Exception
MessageBox.Show("Error in Import to Pdf")
End Try
End Function
Public Sub ExportToPdf(ByVal document As RadDocument, smallpdfcount As Integer)
Dim provider As New PdfFormatProvider
Dim OneSourceFolder As String = GetInstallFolder()
Try
Using output As Stream = File.Create(OneSourceFolder & "\letter" & smallpdfcount & ".pdf")
'Using output As Stream = File.Create(OneSourceFolder & "\letter.pdf")
provider.Export(document, output)
End Using
Catch ex As Exception
MessageBox.Show("Error in ExporttoPdf")
End Try
可以调用垃圾收集器的方式GC.Collect()...可能在生成每个PDF后可以调用它... – lem2802
您如何知道内存实际上未被使用? –
这是关于托管代码吗? – Hristo