Aspose.word 大量生成docx文档后,内存升高,如何控制

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

  1. 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
  1. 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.

  2. Use memory‑optimised loading when you read existing files

LoadOptions loadOptions = new LoadOptions();
loadOptions.setMemoryOptimization(true);
Document doc = new Document("template.docx", loadOptions);
  1. 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.

  2. 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.

:warning: 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)会进一步增加内存使用量,因为渲染需要在内存中构建两个模型:一个用于源文档,另一个用于输出文档。因此,在多次迭代运行期间,由于资源在循环之间无法释放,内存使用量可能会累积。

请问,Aspose.word,关于堆外内存持续升高,无法自动释放的相关问题,有什么更新和优化的进展吗?目前更新到26.2版本,问题依然没有改善。或者是否能提供一些其他方案和建议来控制内存?
内存问题很影响生成环境的运行!希望能尽快优化!
WORDSJAVA-3167 ---- Status : Planned

@dhzhao2016 很遗憾,该问题尚未解决。由此给您带来的不便,我们深表歉意。

这个问题已经非常影响我们生产环境的运行了,这个问题已经很久了,这么严重的问题你们一直不优先解决吗?

@dhzhao2016 我已经安排开发团队尽快查看这个问题。给您带来的不便,我们深表歉意。

从2025年11月,提出堆外内存升高不释放的问题,到现在近半年的时间了,你们解决的怎么样了?还是根本就解决不了?如果解决不了,我们将选择放弃使用Aspose.word产品!

@dhzhao2016 我已经再次请相关开发人员查看此问题。我们会及时向您汇报最新进展。对于给您带来的不便,我们深表歉意。

The issues you have found earlier (filed as WORDSJAVA-3167) have been fixed in this Aspose.Words for Java 26.5 update.

很高兴能收到关于堆外内存不释放问题的修复和更新通知。
经多次生成word测试(每次10000份,每个文档20页左右),内存问题较之前的版本好了很多,但是还有一个问题要请教一下:
1、测试Java程序参数:-Xms1g -Xmx1g -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=512m
2、测试Aspose.word版本
(1)20.2版本,内存增长可控制在1g,并保持稳定
(2)26.5版本,内存增长较之前版本慢了很多,但是还会升高4个g左右,可以保持稳定
相对比20.2版本,26.5版本的内存增长范围是否是正常范围?4个g是否可控?
能否根据参数或者其他方案,将内存增长控制在1g?

@dhzhao2016 Document 类中已实现 clearCaches() 方法。

使用该方法可以更明确、更及时地释放内部资源,从而防止在批量文档处理期间堆内存持续增长。

CleanupOptions cleanupOptions = new CleanupOptions();
cleanupOptions.setUnusedLists(true);
cleanupOptions.setUnusedStyles(true);
cleanupOptions.setUnusedBuiltinStyles(true);
if (doc != null) {
    doc.removeAllChildren();
    doc.cleanup(cleanupOptions);
    doc.cleanup();
    doc.clearCaches();
}

增加了removeAllChildren、cleanup(cleanupOptions)、clearCaches()方法,大量生成文档,内存基本增长到4个G左右,然后可以保持稳定,这种情况是正常的吗?

@dhzhao2016 请您创建一个简单的应用程序,以便我们能够在本地重现您遇到的问题。我们会检查问题并提供更多信息。