2016-08-17 4 views
0

こんにちは、IText HTMLからPDFへのメモリリーク

私はitextを使ってHTMLをPDFに変換しています。しかし、XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);が遅くなり、JVisualVMをチェックすると、メモリリークがあるようです。

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    Document document = new Document(); 
    PdfWriter writer = PdfWriter.getInstance(document, baos);   
    document.open(); 
    InputStream is = new ByteArrayInputStream(content.getBytes()); 
    XMLWorkerHelper.getInstance().parseXHtml(writer, document, is); 

    document.close(); 
    return baos.toByteArray(); 

そのTomcatサーバー上で実行されている:

は、ここに私のコードです。ここで

はhtmlコードは次のとおりです。事前に

<!--?xml version="1.0" encoding="UTF-8"?--> 
 
<html> 
 
<head> 
 
    <title>Title</title> 
 
    
 
    
 
</head> 
 
    
 
<body> 
 
    
 
     
 
EXAMPLE 
 

 
</body> 
 
</html>

感謝。 Mavenのプロジェクトの場合

+1

ほとんどの場合ではありませんメモリが使用されます。または単にメモリー*使用量とは対照的に、実際のメモリー*リーク*の兆候はありますか? – mkl

+0

メモリの使用状況に関するいくつかの統計情報を提供できますか?ドキュメントはどのくらいですか? 1-2枚のA4ページ以上、数百枚のように?写真は関係していますか? – Fildor

+0

@mklあなたが正しいと思います。私はJavaVisualVMをチェックします。ヒープサイズは500MBです。XMLWorkerHelper.getInstance()。parseXHtml(writer、document、is);呼び出します。ヒープサイズは最大2GBに達します。使用されているヒープは約500MB以下です。 – Develofer

答えて

0

:プロジェクトのpom.xmlファイルに次の依存関係を追加します。

<dependency> 
     <groupId>org.apache.poi</groupId> 
     <artifactId>poi-ooxml</artifactId> 
     <version>3.14</version> 
    </dependency> 

    <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf --> 
    <dependency> 
     <groupId>com.itextpdf</groupId> 
     <artifactId>itextpdf</artifactId> 
     <version>5.5.8</version> 
    </dependency> 

    <!-- https://mvnrepository.com/artifact/com.itextpdf.tool/xmlworker --> 
    <dependency> 
     <groupId>com.itextpdf.tool</groupId> 
     <artifactId>xmlworker</artifactId> 
     <version>5.5.8</version> 
    </dependency> 

import java.io.File; 
    import java.io.FileInputStream; 
    import java.io.FileOutputStream; 
    import java.io.IOException; 
    import java.io.InputStream; 
    import java.io.OutputStream; 

    import org.apache.poi.util.IOUtils; 

    import com.itextpdf.text.Document; 
    import com.itextpdf.text.DocumentException; 
    import com.itextpdf.text.pdf.PdfWriter; 
    import com.itextpdf.tool.xml.XMLWorkerHelper; 

    public class HtmlToPdf { 
     public static void main(String[] args) throws DocumentException, IOException { 
      File htmlFile = new File(args[0]); 
      String pdfFileName = "test.pdf"; 
      Document document = new Document(); 
      PdfWriter writer = null; 
      InputStream is = null; 
      OutputStream out = null; 
      if (htmlFile.exists()) { 
       try { 
        is = new FileInputStream(htmlFile); 
        out = new FileOutputStream(pdfFileName); 
        writer = PdfWriter.getInstance(document, out); 
        document.open(); 
        XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);    
        System.out.println("PDF Created!"); 
       } finally { 

        // close the document before before input stream (is) and writer closure     
        if(document != null && document.isOpen()) { 
         document.close(); 
        } 

        // no harm in closing writer here 
        if(writer != null) { 
         writer.close(); 
        } 
        IOUtils.closeQuietly(out); 
        IOUtils.closeQuietly(is); 
       } 
      } 
     } 
    } 
メモリリークが、はるかにある、単に状況
+0

こんにちは。 PDFWriterのパラメータがcloseQuietlyにありません – Develofer

+0

私はWriterのサブクラスであることを期待していました。close()はnullでない場合は最後に呼び出すことができます。 – user3325637

+0

'document'を終了しないことは、iText 5.xではno-goです。空の 'catch'節が一般的です。あなたはあなたのコードを全くテストしましたか?それは、実際にはストリームを閉じるのは良い考えだと言われています。 – mkl