We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Memory leakage when print document using .NET

Hi,

I hope you can help with an ugly memory problem. We have a batch job that creates Aspose.Words documents by grabbing blobs from an Oracle database, and then prints each document and saves it to a network share. This job creates a large number of documents each time it runs.

When I run the batch job, it builds up memory until the batch server runs out of free memory.

When I debug the job, I see that the memory footprint goes up about 10MB every time it prints, and that memory is never freed back up again.

I'm declaring one Aspose.Words.Document object at the beginning of the job as Public, and re-using it for each new document I create. I use three other objects (all declared Public, like the document) in the batch job; DocumentBuilder, AsposeWordsPrintDocument and System.Drawing.Printing.PrinterSettings. I'm gettting rid of the AsposeWordsPrintDocument and PrinterSettings objects each time I print, and calling GC, like this:

m_awPrintDoc = Nothing

m_printerSettings = Nothing

GC.Collect()

After GC.Collect runs, I get about 3MB of memory back, but that leaves about 7MB building up each time I print.

Do I need to declare the Aspose.Words.Document and DocumentBuilder locally, and dispose of them each time? I'd rather not, since I need those objects to persist into another part of the batch job.

Thanks for any help you can give.

Matt

Hi Matt,

Thanks for your query. Please accept my apology for late response. I have tested the scenario and have not faced this issue by using latest version of Aspose.Words for .NET. It would be great, If you share your code and sample documents for investigation purposes.

Thanks for responding. I created the following very simple example. When I debug through this in Visual Studio 2008, I see the memory footprint jump up every time it prints. If I let it continue to print documents, the memory footprint grows steadily. I let it print about 25 documents, and the memory footprint reached almost 100MB.

Thanks,

Matt

Module Module1

Sub Main()

Dim asdoc As Aspose.Words.Document = Nothing

Dim m_awPrintDoc As Aspose.Words.Rendering.AsposeWordsPrintDocument = Nothing

Dim m_printerSettings As System.Drawing.Printing.PrinterSettings = Nothing

Try

For i As Integer = 0 To 100

asdoc = New Aspose.Words.Document("C:\Data\Temp\Temp_0.doc")

m_awPrintDoc = New Aspose.Words.Rendering.AsposeWordsPrintDocument(asdoc)

m_printerSettings = m_awPrintDoc.PrinterSettings

m_printerSettings.PrinterName = \\MyPrinter

m_awPrintDoc.Print()

m_awPrintDoc = Nothing

m_printerSettings = Nothing

asdoc = Nothing

GC.Collect()

Next

Catch ex As Exception

Finally

m_awPrintDoc = Nothing

asdoc = Nothing

m_printerSettings = Nothing

End Try

End Sub

End Module

Hi Matt,

Thanks for sharing the further information. Please share your document as well for investigation purposes.

Here's the document I was printing. Additional info: I'm seeing the memory issue when I'm debugging the code on a Windows7 Enterprise PC, and have also seen the memory issue when running on our batch server, which is a Windows2003 server.

I tried an experiment, which reduced the memory issue by half. I removed the AsposeWordsPrintDocument, and instead used an in-house object (that 's the m_printer object in the code below), that uses Windows API calls to set settings on the default printer. When I run this code, the memory footprint reaches about 50MB after 24 prints, as opposed to 100MB with the previous example code. I'm now wondering if the biggest contributor to the memory issue is the AsposeWordsPrintDocument object.

Module Module1

Sub Main()

Dim asdoc As Aspose.Words.Document = Nothing

Dim m_awPrintDoc As Aspose.Words.Rendering.AsposeWordsPrintDocument = Nothing

Dim m_printer As GOV.WI.DWD.EEDM.PrinterSettingsAPI.SetPrinterSettings = Nothing

Try

For i As Integer = 0 To 100

asdoc = New Aspose.Words.Document("C:\Data\Temp\Temp_0.doc")

m_printer = New SetPrinterSettings("\\DWDMADVPS1\MAD-GEF1-A300-RIMP5001-01-BITS")

m_printer.SetDuplex(SetPrinterSettings.Duplex.SIDE)

m_printer = Nothing

asdoc = Nothing

GC.Collect()

Next

Catch ex As Exception

Finally

m_awPrintDoc = Nothing

asdoc = Nothing

End Try

End Sub

Hi Matt,

I have tested the scenario and have not found any issue with print method. Please use the latest version of Aspose.Words for .NET 11.0.0. Please test your application in release mode not debug mode. In debug mode the visual studio memory allocation is different from release mode.

I wanted to report back that it WAS NOT a memory issue with the Aspose.Words objects. Other non-Aspose.Words objects were being created and not properly disposed of.

The memory footprint of our batch job is big (about 80MB), but doesn't leak now.

Matt

Hi Matt,


It’s great you were able to find the source of the issue in your application. Please feel free to come back and ask any time you need any assistance.

Thanks,