Saxon - 转型时内存不足

时间:2017-10-17 19:38:24

标签: java xml memory heap saxon

在进行转换时,我需要帮助尝试找出导致内存异常的原因。

代码是这样的:

Transformer transformer = cachedXSLT.newTransformer();
String strXMLInput = this.toString();
StringReader xmlReader = new StringReader(strXMLInput);
transformer.transform(new StreamSource(xmlReader), result);

其中cachedXSLT是一个模板,结果是javax.xml.transform.Result

日志显示:

java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOfRange(Arrays.java:4138)
at java.util.Arrays.copyOf(Arrays.java:3870)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:119)
at java.io.PrintStream.write(PrintStream.java:454)
at sun.nio.cs.StreamEncoder$CharsetSE.writeBytes(StreamEncoder.java:355)
at sun.nio.cs.StreamEncoder$CharsetSE.implFlushBuffer(StreamEncoder.java:425)
at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:138)
at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:187)
at java.io.PrintStream.write(PrintStream.java:501)
at java.io.PrintStream.print(PrintStream.java:643)
at java.io.PrintStream.println(PrintStream.java:780)
at net.sf.saxon.StandardErrorListener.error(StandardErrorListener.java:210)
at net.sf.saxon.Controller.recoverableError(Controller.java:865)
at net.sf.saxon.trans.Mode.reportAmbiguity(Mode.java:593)
at net.sf.saxon.trans.Mode.getRule(Mode.java:257)
at net.sf.saxon.trans.RuleManager.getTemplateRule(RuleManager.java:160)
at net.sf.saxon.instruct.ApplyTemplates.applyTemplates(ApplyTemplates.java:329)
at net.sf.saxon.instruct.ApplyTemplates$ApplyTemplatesPackage.processLeavingTail(ApplyTemplates.java:527)
at net.sf.saxon.instruct.ApplyTemplates.applyTemplates(ApplyTemplates.java:317)
at net.sf.saxon.instruct.ApplyTemplates$ApplyTemplatesPackage.processLeavingTail(ApplyTemplates.java:527)
at net.sf.saxon.instruct.ApplyTemplates.applyTemplates(ApplyTemplates.java:317)
at net.sf.saxon.instruct.ApplyTemplates.apply(ApplyTemplates.java:210)
at net.sf.saxon.instruct.ApplyTemplates.processLeavingTail(ApplyTemplates.java:174)
at net.sf.saxon.instruct.Block.processLeavingTail(Block.java:556)
at net.sf.saxon.instruct.Instruction.process(Instruction.java:93)
at net.sf.saxon.instruct.ElementCreator.processLeavingTail(ElementCreator.java:296)
at net.sf.saxon.instruct.Choose.processLeavingTail(Choose.java:686)
at net.sf.saxon.instruct.Block.processLeavingTail(Block.java:556)
at net.sf.saxon.instruct.Instruction.process(Instruction.java:93)
at net.sf.saxon.instruct.ElementCreator.processLeavingTail(ElementCreator.java:296)
at net.sf.saxon.instruct.Block.processLeavingTail(Block.java:556)
at net.sf.saxon.instruct.Instruction.process(Instruction.java:93)
at net.sf.saxon.instruct.ElementCreator.processLeavingTail(ElementCreator.java:296)
at net.sf.saxon.instruct.Block.processLeavingTail(Block.java:556)
at net.sf.saxon.instruct.Template.applyLeavingTail(Template.java:203)
at net.sf.saxon.instruct.ApplyTemplates.applyTemplates(ApplyTemplates.java:345)
at net.sf.saxon.instruct.ApplyTemplates.defaultAction(ApplyTemplates.java:378)
at net.sf.saxon.instruct.ApplyTemplates.applyTemplates(ApplyTemplates.java:333)
at net.sf.saxon.Controller.transformDocument(Controller.java:1807)
at net.sf.saxon.Controller.transform(Controller.java:1621)

分析堆显示堆主要由ByteArrayOutputStream占用

在Introscope中显示的堆使用的堆大约为60%时,可能会生成Java堆空间。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

你在这里提出了同样的问题

https://saxonica.plan.io/issues/3488

我引用了我的回答:

Saxon并没有在内部使用任何ByteArrayOutputStream实例;我认为唯一的例子就是你(可能)提供的用于保存转换结果的实例。结果有多大?为什么要将结果放在ByteArrayOutputStream中?

实际上看一下我们的堆栈跟踪:

at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:187)
at java.io.PrintStream.write(PrintStream.java:501)
at java.io.PrintStream.print(PrintStream.java:643)
at java.io.PrintStream.println(PrintStream.java:780)
at net.sf.saxon.StandardErrorListener.error(StandardErrorListener.java:210)
at net.sf.saxon.Controller.recoverableError(Controller.java:865)
at net.sf.saxon.trans.Mode.reportAmbiguity(Mode.java:593)

所以这里的ByteArrayOutputStream似乎是StandardErrorListener输出的警告消息的目标。您是否重定向了此输出?

你还没有说过你正在使用哪种撒克逊版本:它看起来像旧版本,从行号来判断。

摆脱模板规则中的歧义以避免警告消息可能是个好主意:在模板规则上放置一个优先级属性,其中多个规则可以匹配同一个节点。