在打印和合并 pdf 文件时,将 iText 与 Java 一起使用以避免内存泄漏和不必要的内存消耗的最佳做法是什么

What are the best practices to use iText with Java to avoid memory leaks and unnecessary memory consumption while printing and merging pdf files

我正在使用 iText 2.1.7 库创建 pdf 报告。所有页面涉及的只有两个小日志作为图片内容。其余的都是文本和表格内容。我已经将图像设为单例以避免图像使用额外的内存。这似乎可以正常处理多达 10000 条记录。但除此之外,堆是 space 中的 运行。当前堆大小设置为 1Gb。

在执行 pdf 生成时,我可以做任何其他优化来减少内存消耗吗?

更新 1:添加更多细节和代码。

我的要求是为每条记录打印单独的 pdf 文件,并将它们合并为一个 pdf 文件作为最终输出。为了减少内存使用,我将数据分批加载并在加载下一批之前执行打印+合并。我使用的方法如下:

    // This method is used to print seperate pdf files for each record to be printed. Each record prints a seperate pdf file with 1 to 4 pages containing tabular data.
        public byte[] print(PrintData printData) throws Exception
        {
                ByetArrayOutputStream outputStream = new ByetArrayOutputStream();
                // create a document
                Document document = new Document()
                // create the writer
                pdfWriter = PdfWriter.getInstance(document, outputStream);
                // open the document
                document.open();
                // build the page
                addPdfContents(document, printData);
                // close the document
                document.close();
                pdfWriter.close();
                byte[] printFile = outputStream.toByteArray();
                outputStream.close();
                return printFile;
         }

//the final document and output stream are defined outside as class variables. which will be closed after the final merge operation.

   PdfCopy copier = null;
   ByteArrayOutputStream outputStream = null;

   public void initializeFinalDoc(){
        outputStream = new ByteArrayOutputStream();
        Ddocument document = new Document();
        copier = new PdfCopy(document, outputStream);
        document.open();
   }

    public byte[] getFinalDoc(){
        document.close();
        outputStream.close();
        return outputStream.toByteArray();
    }
// After printing individual files it is merged with the master document to get the final output as a single merged document.
    public void merge(byte[] eachFile) throws Exception
    { 
            if(eachFile != null)
            {
                PdfReader reader = new PdfReader(eachFile);
                PdfImportedPage page;
                int numberOfPages = reader.getNumberOfPages();
                for(int i = 0; i < numberOfPages;)
                {
                    ++i;
                    page = pdfCopier.getImportedPage(reader, i);
                    pdfCopier.addPage(page);
                }
                pdfCopier.freeReader(reader);
            }
        }

我不会将所有内容添加到 table。 与最终文档合并后,pdf 文件的大小似乎在增加。当我打印 5000 条记录时,最终的 pdf 文件有 5000 页和近 400Mb 大小。知道为什么文件的大小变大了吗?我采用的方法有什么问题吗?

更新2:我更新了示例代码。之前的错误还请见谅

我在这里提到我根据评论区的讨论所做的更改。这样这个问题就可以关闭并且对其他人有帮助。

  1. 关闭代码中的所有资源,同时打印和合并内容,并使未使用的引用可用于垃圾收集器。
  2. 如果内容中有重复图片,请尝试使用 PdfSmartCopy 而不是 PdfCopy

PdfSmartCopy attempts to not add the same object twice to the result but instead re-use the copy added first.

希望对您有所帮助。