2

I'm trying to read an XML using JDOM2, I was testing on my computer and it worked without problem. The problem has started when I tried to use this program on a Windows Server 2012 because is giving an error "Heap space" when it tries to get the children from the Document. I tried to add more memory to the virtual machine on Windows Server but it didn't work.

I tried to use the option "SlimJDOMFactory" without success.

The size of the document is almost 150MB, and there is a total of 20.000 elements more or less.

What Can I do ?

Here is a part of my code.

List<prescription> medicamentos = new ArrayList<prescription>();
SAXBuilder builder = new SAXBuilder();
builder.setJDOMFactory(new SlimJDOMFactory()); 
File prescriptionFile = new File("Download" + File.separator + "Prescripcion.xml");
try {
   Document document = (Document) builder.build(prescriptionFile);
   Element rootNode = document.getRootElement();
   List<Element> list = rootNode.getChildren();    

   .........

} catch(Exception a) {
}
trincot
  • 317,000
  • 35
  • 244
  • 286
Jaume
  • 39
  • 1
  • 2

2 Answers2

1

DOM reads the full xml document and load it into your RAM. If you have problems with the size of your RAM you should thinking about to use another xml parsing library like SAX or StAX.

To use one of these will also speed up your code because you can read selectively from your xml file. For further information about the difference have a look at this question: Java: Parsing XML files: DOM, SAX or StAX?

This tutorial explains the usage of all those.

Henry Martens
  • 229
  • 1
  • 10
0

Java will consume as much memory as you allow it to on the Java commandline. Adding memory to the VM only will not allow Java to use that memory.

You need to use the Java Commandline's -Xmx flags to add more memory. Consider adding -Xmx512m to allow Java to use 512MB of memory (which should be more than enough to contain the 128MB XML (which typically takes at least 256MB of character space, plus some more for overheads).

See the Java java commandline help.

rolfl
  • 17,539
  • 7
  • 42
  • 76
  • Thanks for your answer, I tried but it didn't work. I don't know what else I can try, it works in all the computers except on Windows Server, I was wondering if maybe it was a problem with Windows Server and java :( – Jaume Aug 24 '17 at 15:17