By default, GroupDocs.Editor aims to perform computations and complete tasks as quickly as possible. If a task requires a lot of memory, GroupDocs.Editor utilizes it to optimize performance. However, in certain scenarios—such as processing very large documents, running in a 32-bit application limited to 2GiB per process, or operating on a machine with limited free memory—a SystemOutOfMemoryException may occur. To address this issue, the WordProcessingSaveOptions class provides the optimizeMemoryUsage property:
// Getter and Setter for optimizeMemoryUsage
publicbooleangetOptimizeMemoryUsage()publicvoidsetOptimizeMemoryUsage(booleanvalue)
By default, this property is set to false, meaning memory optimization is disabled in favor of achieving the best possible performance. Setting it to true enables an alternative document generation mechanism that significantly reduces memory consumption when generating large documents. However, this comes at the cost of slower generation time when performing the editor.save() method.
Example Usage
The following example demonstrates how to enable memory optimization when saving a large WordProcessing document:
// Import necessary modules
constgroupdocs_editor=require('groupdocs-editor');constfs=require('fs');// Load the source WordProcessing document
consteditor=newgroupdocs_editor.Editor('LargeDocument.docx');// Create WordProcessingEditOptions if needed
consteditOptions=newgroupdocs_editor.WordProcessingEditOptions();// Edit the document to get an EditableDocument instance
consteditableDocument=editor.edit(editOptions);// Create WordProcessingSaveOptions
constsaveOptions=newgroupdocs_editor.WordProcessingSaveOptions();// Enable memory optimization
saveOptions.optimizeMemoryUsage=true;// Save the edited document with memory optimization enabled
editor.save(editableDocument,'OutputDocument.docx',saveOptions);console.log('Document saved successfully with memory optimization enabled.');
Note:
Replace 'LargeDocument.docx' with the path to your large WordProcessing document.
Enabling optimizeMemoryUsage reduces memory consumption but may increase the time it takes to save the document.
Important Considerations
Performance Trade-off: Enabling memory optimization reduces memory usage but may result in slower document generation. Use this option when dealing with very large documents or limited memory environments.
Default Behavior: By default, optimizeMemoryUsage is set to false to prioritize performance.
Exception Handling: If you encounter a SystemOutOfMemoryException or similar memory-related issues, consider enabling this option.
Conclusion
The optimizeMemoryUsage property in WordProcessingSaveOptions offers a way to handle large documents efficiently when using GroupDocs.Editor for Node.js via Java. By adjusting this setting, you can balance memory consumption and performance according to your application’s requirements.