即使页面不包含前向引用和<
,如何避免FOP消耗越来越多的内存。页面序列>块很小?
这是一个测试java程序,它为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个页面序列。
为什么会这样?
答案 0 :(得分:0)
我怀疑,尽管你有sleep
电话,你的制作人的工作速度比你的消费者快得多,而你的管道流正在填满你的记忆。我可以想到两种方法来解决这个问题:
选项1是使用BlockingQueue而不是管道流。
选项2是向public boolean pipeIsFull()
添加Fo2Pdf
方法,如果in.available()
超过,则返回true,我不知道,2mb。然后,如果pipeIsFull()
为真,你的主要for循环将睡眠500毫秒。
此外,减少内存消耗的方法是
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使用的相比),但它不会受到伤害。