簡體   English   中英

FOP-如何避免大量的頁面序列占用大量內存?

[英]FOP - how to avoid high memory consumption with very high number of page-sequences?

即使頁面不包含前向引用並且<page-sequence>塊很小,如何避免FOP占用越來越多的內存?

這是一個Test Java程序,它用手工制作的FO向FOP供料,該FO在相同的非常基本的頁面序列上重復一遍:

Fo2Pdf.java

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import org.xml.sax.helpers.DefaultHandler;

public class Fo2Pdf implements Runnable {

private PipedInputStream in;

public Fo2Pdf(PipedInputStream in)  {
    this.in = in;
}


@Override
public void run() {
    // instantiate Fop factory
    FopFactory fopFactory = FopFactory.newInstance();
    fopFactory.setStrictValidation(false);

    // Setup output
    OutputStream out = null;
    try {
        out = new FileOutputStream("output.pdf");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    try {
        // Setup user agent
        FOUserAgent userAgent = fopFactory.newFOUserAgent();
        userAgent.setConserveMemoryPolicy(true);

        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, userAgent, out);

        // Setup JAXP using identity transformer
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(); 

        // Setup input stream
        Source src = new StreamSource(in);

        // Resulting SAX events (the generated FO) must be piped through to FOP
        DefaultHandler defaultHandler = (DefaultHandler) fop.getDefaultHandler();
        Result res = new SAXResult(defaultHandler);

        // Start FOP processing
        transformer.transform(src, res);

    } catch (Exception e) {
        e.printStackTrace();
    }
    }
}

FeedFo.java

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;


public class FeedFo {


public static void main(String args[]) throws IOException, InterruptedException {

    // instantiate and connect the pipes
    PipedInputStream in = new PipedInputStream();
    PipedOutputStream out = new PipedOutputStream(in);

    // Fo2Pdf - instantiate and start consuming the stream
    Fo2Pdf fo2Pdf = new Fo2Pdf(in);
    Thread fo2PdfThread = new Thread(fo2Pdf, "Fo2Pdf");
    fo2PdfThread.start();

    /*
     <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <fo:layout-master-set>
            <fo:simple-page-master master-name="A4" page-width="210mm" page-height="297mm">
                <fo:region-body/>
            </fo:simple-page-master>
        </fo:layout-master-set>

     */
    out.write(("<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\"><fo:layout-master-set>" +
            "<fo:simple-page-master master-name=\"A4\" page-width=\"210mm\" page-height=\"297mm\">" +
            "<fo:region-body/></fo:simple-page-master></fo:layout-master-set>").getBytes());


    for(int i=0; i<100000000; i++) {

        // sleep 3 seconds every 50000 page-sequences to make sure the consumer is faster than the producer
        if(i % 50000 == 0) {
            Thread.currentThread().sleep(3000);
        }

        /*
         <fo:page-sequence xmlns:fo="http://www.w3.org/1999/XSL/Format" master-reference="A4">
            <fo:flow flow-name="xsl-region-body">
                <fo:block/>
            </fo:flow>
        </fo:page-sequence>
         */
        out.write(("<fo:page-sequence xmlns:fo=\"http://www.w3.org/1999/XSL/Format\" master-reference=\"A4\"><fo:flow flow-name=\"xsl-region-body\"><fo:block/></fo:flow></fo:page-sequence>").getBytes());
    }

    out.write("</fo:root>".getBytes());
    out.flush();
    out.close();

    fo2PdfThread.join();

    System.out.println("Exit");
}
}

如您所見,頁面序列關閉后,FOP會將PDF寫入磁盤。 這意味着(不應該)將頁面保存在內存中。 但是,內存只是在不斷增長。 堆大小為256MB時,生成停止在大約150000頁序列中。

為什么會這樣呢?

我懷疑,盡管您sleep電話,生產者的工作速度仍比消費者快得多,並且管道傳輸流正在占用您的內存。 我可以通過兩種方式解決此問題:

選項1是使用BlockingQueue代替管道流。

選項2是向public boolean pipeIsFull()添加一個public boolean pipeIsFull()方法,如果in.available()超過Fo2Pdf ,則返回true。 然后,您的main for循環將休眠500毫秒,或者如果pipeIsFull()為true,則將休眠。

另外,減少內存消耗的一種方法是

byte[] bytes = ("<fo:page-sequence xmlns:fo=\"http://www.w3.org/1999/XSL/Format\" master-reference=\"A4\"><fo:flow flow-name=\"xsl-region-body\"><fo:block/></fo:flow></fo:page-sequence>").getBytes();
for(int i=0; i<100000000; i++) {
    ...
    out.write(bytes);
}

我不知道這將產生多大的影響(它將減少幾GB,但與Fo2Pdf所使用的相比,這可能是花生),但不會造成傷害。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM