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

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

  1. <Redis Advance><Pipelining><Memory Optimization><Expire><Transactions>

    Overview About Redis pipelining About Redis memory optimization About Redis expire About Redis trans ...

  2. maven nexus memory optimization

    #链接地址:https://help.sonatype.com/repomanager3/system-requirements#filehandles While starting Nexus I ...

  3. config large memory

    C Configuring Large Memory Optimization This appendix provides information for configuring memory op ...

  4. Everything You Always Wanted to Know About SDRAM (Memory): But Were Afraid to Ask

    It’s coming up on a year since we published our last memory review; possibly the longest hiatus this ...

  5. Redis: Reducing Memory Usage

    High Level Tips for Redis Most of Stream-Framework's users start out with Redis and eventually move ...

  6. In-Memory:在内存中创建临时表和表变量

    在Disk-Base数据库中,由于临时表和表变量的数据存储在tempdb中,如果系统频繁地创建和更新临时表和表变量,大量的IO操作集中在tempdb中,tempdb很可能成为系统性能的瓶颈.在SQL ...

  7. C++ 应用程序性能优化

    C++ 应用程序性能优化 eryar@163.com 1. Introduction 对于几何造型内核OpenCASCADE,由于会涉及到大量的数值算法,如矩阵相关计算,微积分,Newton迭代法解方 ...

  8. Oracle启动报错ORA-27102解决

    环境:RHEL5.5 + Oracle 10.2.0.4 此错误一般是因为数据库的初始化参数文件的内存设置不当导致.本例是因为操作系统参数设置问题导致. 当前现象:Oracle启动报错ORA-2710 ...

  9. Which is the best opencv or matlab for image processing?

    http://www.researchgate.net/post/Which_is_the_best_opencv_or_matlab_for_image_processing Annette Mor ...

随机推荐

  1. 通过ASP禁止指定IP和只允许指定IP访问网站的代码

    过ASP禁止指定IP和只允许指定IP访问网站的代码,需要的朋友可以参考下. 一.禁止指定IP防问网站,并执行相应操作: 代码如下: <% Dim IP,IPString,VisitIP '设置I ...

  2. 去蓝港在线面试Unity3D的笔试题。难吗?知道答案的在评论里写出来分享

    前一阵子去蓝港面试unity3d程序,在前台登记以后被领到一个吧台前面填2张个人信息表,之后有人送来笔试题,做了1个小时,感觉挺难的.之后被带到下面面试,面试的是一个年龄稍大的(可能是项目经理或者技术 ...

  3. fuse挂载hdfs目录到linux本地

    1,安装fuse yum -y install hadoop-hdfs-fuse 2.修改环境变量 vi /etc/profile 增加如下配置: JAVA_HOME=/usr/jdk64/jdk1. ...

  4. 在win下面使用cdt+cygwin+cmake

    在cygwin终端下面, cmake -G"Eclipse CDT4 - Unix Makefiles" -D CMAKE_BUILD_TYPE=Debug 当收获警告 Could ...

  5. out ref区别

    1.使用ref型参数时,传入的参数必须先被初始化.对out而言,必须在方法中对其完成初始化. 2.out适合用在需要retrun多个返回值的地方,而ref则用在需要被调用的方法修改调用者的引用的时候. ...

  6. CAF(C++ actor framework)(序列化之类,无需序列化,直接传)(二)

    昨天讲了Struct,还是不够满意,毕竟C++里面类用的比较多嘛,那就先上个类, 这段代码是我稍微改编了一下的结果.都是最基本的用法. #include <utility> #includ ...

  7. Contiki系统介绍

    本文内容来源为contiki英文介绍,自己为了学习,将其大致翻译成中文,以便了解. 欢迎转载,转载请注明来源,如果有什么翻译不合适的地方,请留言指出,相互交流学习. 介绍 Contiki是一个开放源码 ...

  8. JAVA MemCache 史无前例的详细讲解【转】

    非原创转自:http://nhy520.iteye.com/blog/1775893 这篇文章是我看到的介绍的比较详细的,入门级别算是足足够了 Memcach什么是Memcache Memcache集 ...

  9. STL merge的实现细节

    //std::merge的两个版本 template<class InputIt1, class InputIt2, class OutputIt> //First version Out ...

  10. ASP.NET 将DataTable解析成JSON简介

    这里解析json使用的是Newtonsoft.Json.dll程序集.下面请看code: using System; using System.Collections.Generic; using S ...