Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a large XML file with Out Of Memory Error Java

I am developing an app for android, I need to create a rather large XML file.

I have tried this with a DOM document however because it resides in memory it throws an 'out of memory' error.

Can someone suggest an alternative solution? This is a XML file create from scratch.

like image 294
Hank Avatar asked Feb 18 '26 14:02

Hank


2 Answers

I would write the XML to a Stream or Writer progressively as to traverse the data you want to turn into XML. This way you don't need much more memory than the original data.

like image 95
Peter Lawrey Avatar answered Feb 20 '26 02:02

Peter Lawrey


You will always go out of memory for Large Data size. How about dividing your Data and creating small chuncks and then appending all those together.

Or an other option is using SAX parser which consumes less memory.

like image 43
Amandeep Jiddewar Avatar answered Feb 20 '26 04:02

Amandeep Jiddewar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!