Aspose.word for java . Version25.9
com.aspose
aspose-words
25.9
jdk17
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
buidler.insertHtml(“…”);
SaveOptions saveOptions = SaveOptions.createSaveOptions(SaveFormat.DOCX);
saveOptions.setMemoryOptimization(true);
doc.save(“test.docx”, saveOptions);
doc.cleanup();
当大量创建文档,插入html内容并保存后,内存升高,怎么能控制内存增长和释放。
@dhzhao2016
Summary
Creating a new Document inside a loop allocates unmanaged (native) memory for each document. If the native resources are not released, the process’s memory keeps growing even when setMemoryOptimization(true) is enabled.
Why it happens
Document (and DocumentBuilder) allocate native buffers for the document model, fonts, images, etc.
- In Java the garbage collector only reclaims managed objects; the native memory is freed only when the document’s
close() method is called (or when the object is used in a try‑with‑resources block).
doc.cleanup() clears some internal caches but does not release the native memory.
- Leaving the
Document objects reachable (e.g., in a list) prevents their native resources from being disposed, causing a steady increase in memory usage.
What to do next
- Ensure the document is closed after each iteration
// Java 8+ – try‑with‑resources automatically calls Document.close()
for (int i = 0; i < count; i++) {
try (Document doc = new Document()) {
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertHtml(htmlContent);
SaveOptions saveOptions = SaveOptions.createSaveOptions(SaveFormat.DOCX);
saveOptions.setMemoryOptimization(true); // reduces memory during saving
doc.save("output_" + i + ".docx", saveOptions);
// Optional: clear temporary caches
doc.cleanup();
} // doc.close() is invoked here, freeing native memory
}
If you cannot use try‑with‑resources, close the document explicitly:
Document doc = new Document();
// … build and save …
doc.cleanup();
doc.close(); // frees native resources immediately
-
Avoid retaining references to old documents
Make sure no collection (e.g., List<Document>) or static field keeps a reference to a document after the loop iteration finishes.
-
Use memory‑optimised loading when you read existing files
LoadOptions loadOptions = new LoadOptions();
loadOptions.setMemoryOptimization(true);
Document doc = new Document("template.docx", loadOptions);
-
Let the garbage collector handle managed objects
You do not normally need to call System.gc(). If you still observe occasional spikes, a manual GC can be triggered, but it should be a last resort.
-
Check for other retained resources
Verify that large images, custom fonts, or other objects created inside the loop are not stored outside the try‑with‑resources block.
Following these steps should keep the unmanaged memory footprint roughly constant when generating a large number of DOCX files. If memory continues to rise, review the code for any lingering references that might prevent Document.close() from being executed.
I could not fully verify this explanation against the retrieved sources. Please double-check the behaviour in your environment and confirm the exact SDK/version.
Document 对象没有close() 方法呀?
很早之前的20.2版本的内存的增长是可控的,这个版本使用很久了。因为20.2版本不满足的一些bug,升级到了25.9版本。希望在新版本中控制一下内存的增长和释放,现在大量生成doc文档,内存溢出情况很严重。
@dhzhao2016 不幸的是,“Aspose.Words”目前不提供额外的内存控制。我们已经记录了WORDSJAVA-3167强制资源清理问题。对于由此造成的不便,我们深表歉意。