I want to transform XML files using XSLT2, in a huge directory with a lot of levels. There are more than 1 million files, each file is 4 to 10 kB. After a while I always receive java.lang.OutOfMemoryError: Java heap space.
My command is: java -Xmx3072M -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEna bled -XX:MaxPermSize=512M ...
Add more memory to -Xmx is not a good solution.
Here are my codes:
for (File file : dir.listFiles()) {
if (file.isDirectory()) {
pushDocuments(file);
} else {
indexFiles.index(file);
}
}
public void index(File file) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
xslTransformer.xslTransform(outputStream, file);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
System.err.println(e.toString());
}
}
XSLT transform by net.sf.saxon.s9api
public void xslTransform(ByteArrayOutputStream outputStream, File xmlFile) {
try {
XdmNode source = proc.newDocumentBuilder().build(new StreamSource(xmlFile));
Serializer out = proc.newSerializer();
out.setOutputStream(outputStream);
transformer.setInitialContextNode(source);
transformer.setDestination(out);
transformer.transform();
out.close();
} catch (SaxonApiException e) {
System.err.println(e.toString());
}
}