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 PdfPTable, Chapter, 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的更多相关文章
- <Redis Advance><Pipelining><Memory Optimization><Expire><Transactions>
Overview About Redis pipelining About Redis memory optimization About Redis expire About Redis trans ...
- maven nexus memory optimization
#链接地址:https://help.sonatype.com/repomanager3/system-requirements#filehandles While starting Nexus I ...
- config large memory
C Configuring Large Memory Optimization This appendix provides information for configuring memory op ...
- 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 ...
- Redis: Reducing Memory Usage
High Level Tips for Redis Most of Stream-Framework's users start out with Redis and eventually move ...
- In-Memory:在内存中创建临时表和表变量
在Disk-Base数据库中,由于临时表和表变量的数据存储在tempdb中,如果系统频繁地创建和更新临时表和表变量,大量的IO操作集中在tempdb中,tempdb很可能成为系统性能的瓶颈.在SQL ...
- C++ 应用程序性能优化
C++ 应用程序性能优化 eryar@163.com 1. Introduction 对于几何造型内核OpenCASCADE,由于会涉及到大量的数值算法,如矩阵相关计算,微积分,Newton迭代法解方 ...
- Oracle启动报错ORA-27102解决
环境:RHEL5.5 + Oracle 10.2.0.4 此错误一般是因为数据库的初始化参数文件的内存设置不当导致.本例是因为操作系统参数设置问题导致. 当前现象:Oracle启动报错ORA-2710 ...
- 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 ...
随机推荐
- Android PullToRefresh下拉刷新控件的简单使用
PullToRefresh这个开源库早就听说了,不过一直没用过.作为一个经典的的开源库,我觉得还是有必要认识一下. 打开github上的网址:https://github.com/chrisbanes ...
- 牢记!SQL Server数据库开发的二十一条注意点
如果你正在负责一个基于SQL Server的项目,或者你刚刚接触SQL Server,你都有可能要面临一些数据库性能的问题,这篇文章会为你提供一些有用的指导(其中大多数也可以用于其它的DBMS). ...
- 第一篇、微信小程序_01计算器
官方文档地址:https://mp.weixin.qq.com/debug/wxadoc/dev/index.html 一.计算器的首页布局 第一部分WXML: <view class=&quo ...
- C#学习笔记之线程 - 使用线程
基于事件的异步模式 (EAP -- The Event-Based Asynchronous Pattern) EAP提供了一个简单的办法,通过它的类能够提供多线程能力,而不需显式的开启或管理线程.它 ...
- poj 2154 Color
这是道标准的数论优化的polya题.卡时卡的很紧,需要用int才能过.程序中一定要注意控制不爆int!!!我因为爆intWA了好久=_=…… 题目简洁明了,就是求 sigma n^gcd(i,n):但 ...
- ALI OSS RequestTimeTooSkewed
php版阿里oss sdk,请求时抛RequestTimeTooSkewed错误,说时间差距太大,搜了一下发现是服务器的时间设置问题. 我们在安装完Centos Linux操作系统之后,点击系统的时间 ...
- Android 布局 中实现适应屏幕大小及组件滚动
要实现如图的布局: 这是在eclipse可视化窗口中的截图,但实际运行在Android设备上可能出现的问题有: (1):当编辑图1中的最后一个EditText时,输入法的编辑界面会把底部的Button ...
- C# 实现HTML5服务器推送事件
为什么需要服务器推送事件: 因为如果需要保持前台数据的实时更新例如,IM聊天,股票信息, 1.可以在客户端不断地调用服务端的方法来获得新数据,但是这样会很消耗服务器资源,导致系统变慢! 2 html5 ...
- HTML5如何重塑O2O用户体验
低频次垂直O2O服务应该继续开发原生APP吗?大家有没有发现做一个APP的推广成本和获取用户的成本越来越高?第二,用户安装APP之后,用户并不是经常点击使用APP的,那这是为什么?数据表明90%的O2 ...
- php获取客户端ip地址
本文介绍一个,php获取客户端的IP地址的实例代码,有需要的朋友参考下吧. 获取客户端IP地址的代码,如下: 复制代码代码示例: <?php//取得客户端IP的函数function get_cl ...