-2

I am trying to generate a large amount (>5000) of pdf documents dynamically. The problem is during the pdf generation I get an out of memory exception. I have decided to break up my 5000 page pdf into smaller 500 pdf's but when I try it this way .net still uses the same memory. Is there a way to force my application to free unused memory that a variable is still using? Here is the code causing the problem

 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
wrj
  • 19
  • 1
  • 5

1 Answers1

3

I don't think the issue is that .net keeps unused memory. If .net runs low on memory, it will trigger the garbage collection no matter what. As this is not the case for you, I guess the real problem is that you keep references to the objects you want to get out of the memory.

So what you should look for is:

  • What objects use that much memory?
  • Are those objects stored permanently in the memory anywhere? Look for any collections that keep references to your objects.
  • Are there any objects that implement the IDisposable interface that are not properly disposed? (Either by calling Dispose() or by a Using block.
  • Are there any MemoryStreams or anything that writes to the harddisk around that that is not closed properly?
  • Are you clearing the references to the source objects you use to fill the PDF files once you are done with them?
  • Do you use any cache that could fill up?

Hope that gives you a place to start looking. Calling GC.Collect() to force the garbage collection is usually the wrong way, because .NET should handle this properly.

Nitram
  • 6,486
  • 2
  • 21
  • 32