views:

94

answers:

1

Hi, I am getting Out of Memory error:

Memory Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at org.apache.xerces.dom.CoreDocumentImpl.createElement(CoreDocumentImpl.java:564)

I have a standalone Java program which fetches data from DB and create an XML file using DOM. I get the above error if the data fetched is huge, in my case it is > 1,000,000 records.

I have defined 2GB as heap size while calling the Java class from unix.

I tried it with JAXB, but still do not any significant improvement.

Any suggestions how to improve the code.

+5  A: 

You should probably avoid loading the entire file into memory at once. To do this, switch from using DOM to another technology such as SAX or StAX. It is a streaming APIs and thus are more suited for handling huge amounts of XML data.

Edit: SAX doesn't support writing, therefore it's not applicable here.

Joachim Sauer
@ Joachim - I tired with JAXB, but don't see any improvement. Can you provide some sample code how to use SAX. I am novice to this.
Anurag
Try this tutorial: http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JAXPSAX.html
Joachim Sauer
I didn't suggest JAXB, but if you really want to use it, then [this document](https://jaxb.dev.java.net/guide/Dealing_with_large_documents.html) might give useful hints.
Joachim Sauer
The SAX tutorial you given is about parsing the existing XML.I am trying to create XML file with data fetched from DB.
Anurag
I seem to have missed the "writing XML" part. In that case SAX doesn't help, however StAX **does** support writing XML in streaming mode, so that would be your best bet.
Joachim Sauer
Yes, I am trying with StAX now. do you know how to validate the xml file created against an xsd file.
Anurag
@Anurag: sure. And if you ask this in a separate question (or better yet: search for the answer on this site), then others can use the answer(s) as well!
Joachim Sauer