itext 落雨 out of membery Memory Optimization

Memory Optimization

 

If a document deals with a lot of data or large elements, such as images, it is not wise to build the sections entirely in memory and then add them to the document. If you take a look at the code above and run it with 1,000,000 rows, you will run into an OutOfMemoryError!  You can try this for yourself by downloading the source code linked above. This is where the LargeElement interface that the PdfPTableChapter, and Sectionclasses implement, comes into play. Classes implementing the LargeElement interface can be added to the document before they are complete.This is achieved by setting the complete property to false.

 

void setComplete(boolean complete)

If you invoke setComplete(false), you indicate that the content of the object isn’t complete yet; it can be added to the document partially, but more will follow. If you invoke setComplete(true), you indicate that you won’t add any more data to the object.Once this is done, the element can be repeatedly added to the document, releasing memory that the added portion used. More information about the LargeElement interface can be found in the API Docs and this article with examples.Combining The TechniquesIt gets complex when you want to combine memory optimization and element grouping in scenarios that are not trivial.  The following code snippet modifies the previous example to show how to combine techniques.

public static void memoryOptimizedElementGrouping(String filename, int rows)
  throws DocumentException, IOException {
    Document document = new Document(); // Create a document.
    PdfWriter.getInstance(document, new FileOutputStream(filename)); //Setup the writer
    document.open(); // Open the document

    PdfPTable mainTable = new PdfPTable(columns); // Main table of data
    mainTable.setComplete(false);
    PdfPTable alias = mainTable; //Alias to use for adding content.
    for (int i = 0; i < rows; ++i) {
        if (i == rows/2) {                    // Group at halfway point
            alias = new PdfPTable(columns); // Re-alias to new group table
            alias.setKeepTogether(true);
        }
        if (i == rows/2 + 5) {                // Add group 5 rows later.
            PdfPCell groupCell = new PdfPCell(alias); // Create the cell for group table
            groupCell.setColspan(columns); //Set it to span the entire mainTable
            mainTable.addCell(groupCell); //Add the group table to the main table
            alias = mainTable;
        }
        if (alias == mainTable && i % 10 == 0) {  // If no longer grouping
            document.add(mainTable);              // and i divisible by 10,
        }                                         // Add to the document
        alias.addCell(new Phrase("Left Cell "+i));
        alias.addCell(new Phrase("Right Cell "+i));
    }

    mainTable.setComplete(true);  //Set the table as complete
    document.add(mainTable);      //Add the mainTable to the document for last time.
    document.close();             //Close the document.
}

First notice that the mainTable has its complete property set to false.  The main difference in the loop from the the last example is that the grouping happens in the middle of the table for five rows.  The memory optimization occurs in the third if block. The key point is to first check that alias is pointing to the mainTable. If you have not added your group to the mainTable and try adding the mainTable to the document, you will not get any new data from the group with subsequent additions of the mainTable. After the loop, the mainTable has its complete property to true, marking it as finished.  It can then be added to the document for a final time. If you run the source code, you can see that this second example can be run with 1,000,000 rows without causing an OutOfMemoryError.

 

转自:http://jandyco.com/advanced-itext/

 

另自己也有解决方案:

//for循环中添加如下代码
int _MAX_ROWS = 1000;//最大行数,之后清理
int row_count = 0;//初始值
if (++row_count % _MAX_ROWS == 0) {
                           //datatable是我的一个PdfPTable的new出来的一个实例
                           // add table to Document
                           document.add(datatable);
                           // delete _MAX_ROWS from table to free memory
                           datatable.deleteBodyRows();
                           // let iText manage when table header written
                           datatable.setSkipFirstHeader(true);//防止释放后一页出现两次表头。
}

详细参阅:

http://blog.csdn.net/ae6623/article/details/11590611

 

时间: 2024-10-02 02:54:19

itext 落雨 out of membery Memory Optimization的相关文章

揭开Windows XP启动如飞的秘密

1.禁用外设,加速启动 在WinXP中暂时禁用一些外设,可以有效地减少系统启动时需要调入的外设驱动程序数量,从而加快系统的启动速度,因为WinXP在启动时会自动扫描硬件的变化.首先打开该设备的电源,然后打开"设备管理器"窗口,单击工具栏中的"扫描硬件改动"按钮,或者直接用鼠标右键单击已禁用的设备,在弹出的快捷菜单中选择"启用"即可. 2.用软件,让WinXP启动如飞 微软提供了一个专用来加速WinXP启动的补丁程序──BootVis,可到微软网站

《Redis官方文档》 FAQ

原文地址 译者:zivyu 为什么Redis与其他的k-v存储相比不一样 有两个主要的原因 redis在键-值数据库中是一个不同的发展方向,值可以包含更复杂的数据类型,同时许多原子操作定义在这些数据类型上.redis的数据类型和基本数据结构密切相关,没有额外的抽象层,同样对于程序员也是直接可见的. redis是一个在内存中但是可以持久化到磁盘上的数据库,所以它代表了一个不同的权衡,高速读写被实现,但是对数据集有限制,那就是不能大于内存的大小.在内存中的数据库还有另外的优点,内存中表示的复杂数据结

Practical Guide to STL By Jeff Bogan

Introduction STL (Standard Template Library) is a good skill for anyone programming C++ in the modern day. I must say that it takes some getting used to, i.e. there is a fairly steep learning curve, and some of the names that are used are not very in

C++ 应用程序性能优化

C++ 应用程序性能优化 eryar@163.com 1. Introduction 对于几何造型内核OpenCASCADE,由于会涉及到大量的数值算法,如矩阵相关计算,微积分,Newton迭代法解方程,以及非线性优化的一些算法,如BFGS,FRPR,PSO等等用于多元函数的极值求解,所以这些数值算法的性能直接影响系统的性能.软件的性能优化是计算机软件开发过程中需要一直关注的重要因素,因此有必要学习下C++应用程序性能优化的方法. 在网上寻找相关资料时,发现这方面的资料也很少,最后发现一本由电子

【Itext】解决Itext5大并发大数据量下输出PDF发生内存溢出outofmemery异常

  尼玛,这个问题干扰了我两个星期!!   关键字 itext5 outofmemery 内存溢出 大数据 高并发 多线程 pdf 导出 报表 itext 并发   在读<<iText in Action 2nd>4.3节(Dealing with large tables)的时候,书上写道:itext5PdfPTable实现了ILargElement的接口,只需要我们手动设置datatable.setComplete(false);之后,它就可以自动将表格元素输出到document中,

【java Itext Pdf】itext pdf隔行换色 itext5添加表格背景颜色

Itext5 pdf 行变色效果图: 新需求,隔行换色,itext in action 是个很好的说明书,照着英文读下来,很简单的进行了实现,思路如下:   1.先创建PdfPTable对象,生成PDF表格cell之后,添加隔行换色的事件,将此事件在PdfPTable加入Document对象之前,插入进去 2.隔行换色的事件需要自己写一个java类,里面去定义背景颜色和长宽高,实质就是在pdf表格生成之后,去读取当页page内的所有行和列,并创建一个矩形,加入背景,覆盖到cell内,达到背景有颜

Off-heap Memory in Apache Flink and the curious JIT compiler

Running data-intensive code in the JVM and making it well-behaved is tricky. Systems that put billions of data objects naively onto the JVM heap face unpredictable OutOfMemoryErrors and Garbage Collection stalls. Of course, you still want to to keep

Operating Principle and Implementation of Flink: Memory Management

Nowadays, open-source big data frameworks (such as Hadoop, Spark and Storm) all employ JVM, and Flink is one of them. JVM-based data analysis engines all need to store a large amount of data in the memory, so they have to address the following JVM is

Java Tools: Source Code Optimization and Analysis[转]

Below is a list of some tools that can help you examine your Java source code for potential problems: 1. PMD from http://pmd.sourceforge.net/ License: PMD is licensed under a "BSD-style" license PMD scans Java source code and looks for potential