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

发布于 2024-10-23 18:26:23 字数 4263 浏览 6 评论 0原文

我们已经实现了 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 

我尝试过 Google iText 内存性能增强方法,但不走运!请指教!!!顺便说一句...使用 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 造成的!

We have implemented xHTML-to-PDF conversions to facilitate printable versions of a few of our webpages using iText. Below is the code to create the 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) {}
}

Using Eclipse MAT, I am seeing the following taking 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 

I have tried Googling iText memory performance enhancement methods but have been unlucky!! Plz advice!!! BTW... will using renderer.finishPDF() work??

The following command is used to run 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

Thanks in advance...

EDIT:

PS: I am assuming this is due to iText!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半仙 2024-10-30 18:26:23

renderer.finishPDF() 应该可以达到您提到的目的。

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

renderer.finishPDF() should do the trick as you mentioned.

If the page you're trying to render has a lot of images, you might need to increase your maximum memory a bit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文