Reputation: 301
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.
Upvotes: 2
Views: 2963
Reputation: 308149
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.
Upvotes: 6