Getting java heap space error- while generating word document which size is more than 50MB

Hi,

I am getting java heap space error, while generating the word document, which size is more than 50MB.

Below is the error:
org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.OutOfMemoryError: Java heap space.

My code snippet:

Document doc = new Document("C:\image\Image.doc");
OutputStream stream = new FileOutputStream("C:\image\Image - Copy.doc");
try
{
    doc.save(stream, SaveFormat.DOC);
}

I am using tomcat 6 to run my application. i have attached my tomcat heap java setting.

Is there any other setting need to be followed to avoid this error?
Is there any other alternative to generate the document template, which size is more than 50MB?

Thanks in Advance.

Hi
Rajesh,

Thanks for your inquiry.

Most likely ‘OutOfMemoryError’ occurs when the system is running out of memory (less space in RAM is available for process to continue); in this case, you should note that usually Aspose.Words needs few times more memory than document size to build model of the document (DOM) in memory. For example if your document’s size is 1 MB, Aspose.Words needs 10-20 MB of RAM to build its DOM in memory. Multiplier depends on format because some formats are more compact than others. For example DOCX format is more compact than DOC and RTF, and DOC is more compact than RTF.

Moreover, I think, you can try specifying DocSaveOptions.TempFolder (temporary files folder for reducing the memory usage) upon saving huge documents.

Please let us know if you need more information, we are always glad to help you.

Best Regards,

Hi,

Thanks for your reply.

I have 4GB RAM in my working machine.

Could you please give me some code snippet to use DocSaveOptions.TempFolder. so that i can try and tell you some information?

Hi
Rajesh,

Thanks for your inquiry. You can use the following code snippet to specify the folder for temporary files used when saving to a DOC:

Document doc = new Document(@"c:\temp\in.docx");

DocSaveOptions saveOptions = new DocSaveOptions();
saveOptions.SaveFormat = SaveFormat.Doc;
saveOptions.TempFolder = @"c:\temp";

doc.Save(@"c:\temp\out.docx", saveOptions);

When Aspose.Words saves a document, it needs to create temporary internal structures. By default, these internal structures are created in memory and the memory usage spikes for a short period while the document is being saved. When saving is complete, the memory is freed and reclaimed by the garbage collector.

If you are saving a very large document (thousands of pages) and/or processing many documents at the same time, then the memory spike during saving can be significant enough to cause the system to throw OutOfMemoryException. Specifying a temporary folder using TempFolder will cause Aspose.Words to keep the internal structures in temporary files instead of memory. It reduces the memory usage during saving, but will decrease the save performance.

The folder must exist and be writable, otherwise an exception will be thrown. Also, Aspose.Words automatically deletes all temporary files when saving is complete.

If we can help you with anything else, please feel free to ask.

Best Regards,

Hi,

Thanks for your quick reply.

I am using word 2003 template document.
I had used below code snippet to generate the document, but still am getting the same heap space error.

Document doc = new Document("E:\Image\Image.doc");
DocSaveOptions saveOptions = new DocSaveOptions();
saveOptions.setSaveFormat(SaveFormat.DOC);
saveOptions.setTempFolder("C:\Image\test.doc");
doc.save("E:\Image\test.doc", saveOptions);

I have enough memory in RAM and hard disk.
Please give any alternative solution to solve heap space error.

Thanks in Advance.

Hi
Thanks for your request. Unfortunately, currently there is no way to process such large documents using Aspose.Words without utilizing much memory. I suppose, 50MB DOC file is simply too large. Of course it would be great if you share this document with us so that we can play with it.
Best regards,

Hi,

Thanks for your reply.

I tried to attach the 65MB size document. but i couldn’t. So just you create a document, which contains more images, it will come your expected size.

I am not sure, whether “saveOptions.setTempFolder(“C:\temp\test.doc”);” code working fine or not.

Do i need to increase tomcat heap java size?

Thnaks in Advance…

Hi
Rajesh,

Thanks for your inquiry. To be able to send your document to us, please try using any free file sharing service e.g. SkyDrive. Once we have your input Word document, we will investigate it on our side and provide you more information.

Best Regards,

Hi,
Thanks for your reply.

We could not able to attach 50MB size file in any free sharing service, since its blocked here. Please create a template and paste more images, it will give you the expected size.

Please reply as soon as possible. its urgent.

Regards,
Rajesh P

Hi Rajesh,

Thanks for your inquiry.

Sometimes Aspose.Words for Java will throw OutOfMemoryError on Java. This is because the default Java heap space might not be enough for high memory spikes when processing some documents. In this case, the easy way to workaround is to increase the heap space for the JVM.

I hope, this will help.

Best Regards,