Aspose.word for java . Version25.9
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>25.9</version>
<classifier>jdk17</classifier>
</dependency>
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强制资源清理问题。对于由此造成的不便,我们深表歉意。
请问,Aspose.word,更新到25.12版本后,堆外内存持续升高问题,还是没有解决,内存依然持续升高。这个问题什么时间能解决?
@dhzhao2016 很遗憾,问题尚未解决。我们会及时更新进展,并在问题解决或有更多信息时通知您。由此给您带来的不便,我们深表歉意。
请问,Aspose.word,更新到26.1版本后,堆外内存持续升高问题,还是没有解决,内存依然持续升高。这个问题什么时间能解决?问题很严重影响生成环境运行!!!
@dhzhao2016 遗憾的是,与此主题相关的问题尚未解决。由于 Aspose.Words for Java 的特殊性,加载文档还会初始化大量静态对象,这些对象会占用内存,只有在程序终止或类加载器销毁后才会释放。此外,我们观察到,处理包含大量图形元素和表格的大型复杂文档也会影响内存消耗。保存为固定页数格式(例如 PDF)会进一步增加内存使用量,因为渲染需要在内存中构建两个模型:一个用于源文档,另一个用于输出文档。因此,在多次迭代运行期间,由于资源在循环之间无法释放,内存使用量可能会累积。