2011-03-18 150 views
2

我們實施了xHTML到PDF的轉換,以促進使用iText的幾個我們的網頁的可打印版本。下面是創建PDF的代碼:iText填充我的堆大小導致應用程序崩潰與OOM

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的意見!順便說一句...將使用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的! !

+0

'ITextRender'從哪裏來?我沒有看到它列在http://api.itextpdf.com/ – 2011-03-18 16:04:10

+0

@matt:我正在使用'org.xhtmlrenderer.pdf.ITextRenderer' – Sumit 2011-03-18 16:55:44

+1

iTextRenderer是Flying Saucer和HTML-> PDF轉換器的一部分。 – 2011-03-18 17:02:39

回答

1

renderer.finishPDF()應該照你所說的去做。

如果您嘗試渲染的頁面有很多圖像,則可能需要稍微增加最大內存。

+0

感謝您的提示...將嘗試和更新! – Sumit 2011-03-18 18:41:26

+0

這並沒有解決問題!雖然現在在堆中,我沒有看到任何'org.xhtmlrenderer。*'對象,'org.apache.xerces.dom.ElementNSImpl'對象正在產生問題('Number of Objects:'188,932;'Used Heap Size:' 22,671,840;'保留的堆大小:'1,059,390,176) – Sumit 2011-03-28 10:40:50

相關問題