iText填充我的堆大小导致应用程序崩溃与OOM

时间:2011-03-18 15:30:13

标签: java jboss itext out-of-memory flying-saucer

我们已经实施了xHTML到PDF的转换,以便使用iText促进我们的一些网页的可打印版本。以下是创建pdf的代码:

response.setContentType("application/pdf");
response.setHeader("content-type", "application/pdf");

ITextRenderer renderer = null;
ServletOutputStream out = null;
ByteArrayOutputStream os = null;
try {
    os = new ByteArrayOutputStream();
    renderer = new ITextRenderer();
    renderer.setDocument("http://www.url.com");
    renderer.layout();
    renderer.createPDF(os);
    out = response.getOutputStream();
    out.write(os.toByteArray());
} catch(Exception e) {
    log.error("Error occurred while creating pdf", e);
} finally {
    try {
        if(os != null) {
            os.flush();
            os.close();
        }
    } catch(Exception e) {}
    try {
        if(out != null) {
            out.flush();
            out.close();
        }
    } catch(Exception e) {}
}

使用Eclipse MAT,我看到以下1.2GB:

Class Name                                                          Shallow Heap    Retained Heap   Percentage
java.lang.Thread @ 0x2aaac31fdd38 activeThread-6                    168             1,335,868,960   86.43% 
    org.xhtmlrenderer.layout.LayoutContext @ 0x2aaad4af4358         152             63,981,672      4.14% 
    org.xhtmlrenderer.layout.SharedContext @ 0x2aaad13d92f0         152             36,439,120      2.36% 
    org.xhtmlrenderer.render.BlockBox @ 0x2aaaf9130c88              264             7,742,120       0.50% 
    org.xhtmlrenderer.css.style.CalculatedStyle @ 0x2aaad21ada60    96              7,341,800       0.47% 
    org.xhtmlrenderer.render.BlockBox @ 0x2aaafd7f7cc0              264             3,868,920       0.25% 
    org.xhtmlrenderer.css.style.CalculatedStyle @ 0x2aaaf06a1c58    96              3,701,320       0.24% 
    org.xhtmlrenderer.render.BlockBox @ 0x2aab002761a8              264             1,937,680       0.13% 
    org.xhtmlrenderer.layout.Layer @ 0x2aaad4af43f0                 136             1,910,640       0.12% 
    org.xhtmlrenderer.css.style.CalculatedStyle @ 0x2aaaf9153760    96              1,851,384       0.12% 
    org.xhtmlrenderer.render.BlockBox @ 0x2aab013cdab8              264             968,320         0.06% 
    org.xhtmlrenderer.css.style.CalculatedStyle @ 0x2aaafd7f7598    96              926,520         0.06% 
    org.xhtmlrenderer.render.BlockBox @ 0x2aab01cbb710              264             484,016         0.03% 
    org.xhtmlrenderer.css.style.CalculatedStyle @ 0x2aab00275a80    96              464,088         0.03% 
    org.xhtmlrenderer.render.BlockBox @ 0x2aab020e5be0              264             242,032         0.02% 
    org.xhtmlrenderer.css.style.CalculatedStyle @ 0x2aab013cd390    96              234,888         0.02% 
    org.xhtmlrenderer.render.BlockBox @ 0x2aaad4b25228              264             140,280         0.01% 
    org.xhtmlrenderer.render.LineBox @ 0x2aab0294a8f8               224             139,744         0.01% 
    org.xhtmlrenderer.layout.Layer @ 0x2aab02947d58                 136             139,656         0.01% 
    org.xhtmlrenderer.render.BlockBox @ 0x2aab02306478              264             121,352         0.01% 
    org.xhtmlrenderer.css.style.CalculatedStyle @ 0x2aab01cbafe8    96              118,272         0.01% 

    Total: 20 entries                                               3,584           132,753,824     0.086 

我曾尝试使用谷歌搜索iText内存性能增强方法,但一直不走运!! Plz建议!!! BTW ......将使用renderer.finishPDF()工作??

以下命令用于运行jboss:

java -Dprogram.name=run.sh -server -Xms256m -Xmx1024m -XX:PermSize=64m -XX:MaxPermSize=256m -verbose:gc -Xloggc:/data1/logs/jboss/GC.log -XX:+HeapDumpOnOutOfMemoryError -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 -Dorg.apache.catalina.STRICT_SERVLET_COMPLIANCE=false -Djava.net.preferIPv4Stack=true -Djava.library.path=/usr/local/java/jboss-4.2.2.GA/bin/native -Djava.endorsed.dirs=/usr/local/java/jboss-4.2.2.GA/lib/endorsed -classpath /usr/local/java/jboss-4.2.2.GA/bin/run.jar:/usr/local/java/jdk1.6.0_06/lib/tools.jar org.jboss.Main -c default -b <IP_ADDRESS> -Djboss.messaging.ServerPeerID=1

提前致谢...

修改

PS:我假设这是因为iText !!!

1 个答案:

答案 0 :(得分:1)

renderer.finishPDF()应该像你提到的那样做。

如果您尝试渲染的页面包含大量图像,则可能需要稍微增加最大内存。