Please check below the analysis details of WORDSJAVA-2067:
com.aspose.words.DocumentBuilder#insertImage(java.io.InputStream) is fairly good solution. However,
com.aspose.words.DocumentBuilder#insertImage(java.lang.String) demonstrates slightly better speed. While
com.aspose.words.DocumentBuilder#insertImage(byte) is almost the same as the latter.
But avoid usage of
com.aspose.words.DocumentBuilder#insertImage(java.awt.image.BufferedImage). This method allocates additional memory for intermediate buffers (Java’s
BufferedImage doesn’t keep information of original image file format, so we try to keep image quality at its best at the price of memory consumption).
This option saves approximately 20MB of heap memory. But it doesn’t affect image insertion.
Returning to your example, we had 1500 files. Each file had size of
1 156 447 bytes.
1500 files consumed at least
1 734 670 500 bytes in heap. Even taking into account overhead of Java (and Aspose) classes we should fit into 2GB of the heap (e.g.
-Xms256m -Xmx2048m ). And we did actually. But we adjusted Garbage Collector settings for achieving it.
Thus, the following setting made all of 1500 images inserted via each of insert -method (except
com.aspose.words.DocumentBuilder#insertImage(java.awt.image.BufferedImage) - see the caution above):
-XX:NewRatio=3 - for Parallel Garbage Collector
-XX:+UseG1GC -XX:+UnlockExperimentalVMOptions -XX:G1HeapRegionSize=4m - for G1 Garbage Collector
In general, high memory consumption is caused by the nature of Word documents (among others). Such a document cannot be processed in batch mode, e.g. the whole document should be built in memory before saving it to a disk (with all of the images).