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.

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

 

目录
相关文章
|
10月前
|
SQL Oracle 数据可视化
译|Thinking Clearly about Performance (Part 1)(下)
译|Thinking Clearly about Performance (Part 1)(下)
84 0
|
6月前
|
Java Android开发
Java Heap Space: Understanding and Resolving Memory Issues
Java Heap Space: Understanding and Resolving Memory Issues
|
10月前
|
SQL 资源调度 Oracle
译|Thinking Clearly about Performance (Part 2)(下)
译|Thinking Clearly about Performance (Part 2)(下)
29 0
|
并行计算
Hint: This means that multiple copies of the OpenMP runtime have been linked into the program.
Hint: This means that multiple copies of the OpenMP runtime have been linked into the program.
135 0
PAT (Advanced Level) Practice:1~3题
​ ✨欢迎您的订阅✨ PAT乙级专栏(更新完毕): 👉🏻【Basic Level】👈🏻 PAT甲级专栏(更新ing): 👉🏻【Advanced Level】👈🏻 ​
PAT (Advanced Level) Practice:1~3题
PAT (Advanced Level) Practice - 1022 Digital Library(30 分)
PAT (Advanced Level) Practice - 1022 Digital Library(30 分)
103 0
|
存储 编解码 固态存储
Performance optimization with Lucene4.0
假期重新把之前在新浪博客里面的文字梳理了下,搬到这里
97 0
|
SQL 编译器 API
Efficiently Compiling Efficient Query Plans for Modern Hardware 论文解读
这应该是SQL查询编译的一篇经典文章了,作者是著名的Thomas Neumann,主要讲解了TUM的HyPer数据库中对于CodeGen的应用。 在morsel-driven那篇paper 中,介绍了HyPer的整个执行框架,会以task为单位处理一个morsel的数据,而执行的处理逻辑(一个pipeline job)就被编译为一个函数。这篇paper则具体讲如何实现动态编译。
378 0
Efficiently Compiling Efficient Query Plans for Modern Hardware 论文解读
|
SQL 监控 算法
Adaptive Execution of Compiled Queries 论文解读
本篇是TUM的内存数据库HyPer针对compile-based执行框架的改进。其中涉及到HyPer的动态编译和并行执行框架 动态编译文章的结尾提到了编译执行系统存在的2个问题,其中之一就是:不可控的编译时间。
431 0
Adaptive Execution of Compiled Queries 论文解读
|
算法 前端开发 调度
The Cascades Framework for Query Optimization
这篇paper是前一篇Volcano optimizer的后续,其涉及的概念和优化思路是一脉相承的,在阅读本篇之前,最好对Volcano optimizer有足够的了解,详见文章Volcano优化器框架。
430 0