Mahmoud Ismail
Mahmoud Ismail

Reputation: 187

OutOfMemoryError when parsing large xml file using xstream

I'm facing a problem while reading large XML files "100 MB" and parsing it using xstream always the below error occurs

java.lang.OutOfMemoryError: Java heap space error occurs,

here is the code that parses the XML

XStream xstream = new XStream();
xstream.processAnnotations(Class.forName((String)ClassName));

ClassName is a normal class that have the fields with xml annotations.

then use

Object resultDto = xstream.fromXML((String)fileString);

fileString: is the xml file after reading it as inputstream and put it in string buffer.

the above code works fine with small files but didn't work with the big one, any ideas please?

Upvotes: 3

Views: 2216

Answers (5)

Shailendra
Shailendra

Reputation: 9102

Do not convert your file into a String and then use Xstream, instead use the input stream or file reader directly. XStream.fromXML takes various input like File, InputStream, FileReader that operate directly without the need to first loading the text representation of File in memory as String.

Upvotes: 2

Jay
Jay

Reputation: 9479

How much is the RAM size ? If you have more RAM allocate more to your JVM using -

Xms512m -Xmx2g -XX:PermSize=512m  -XX:MaxPermSize=2g

If you have 8GB of RAM, you could allocate as in the above example it allocate 2GB max Heap memory and 2GB Maximum PermGen space, so in total its 4GB. Or accordingly as per your RAM

Upvotes: 0

Mikkel Løkke
Mikkel Løkke

Reputation: 3749

Well there are 2 solutions as far as I can see:

  1. Increase the heap space.
  2. Use a SAX parser instead of a DOM parser (not sure if you can do this with xstream).

Lastly the data might simply be too large to fit.

Upvotes: 1

Mitul Maheshwari
Mitul Maheshwari

Reputation: 2647

Try to incrise your java head memory. if you are using eclipse. then there is eclipse,ini file is there,

in that incrise the head size.

-XX:MaxPermSize=1024m
-Xms512m
-Xmx1024m

Upvotes: 0

CsBalazsHungary
CsBalazsHungary

Reputation: 813

Can you run your parsing program with runtime parameters? In that case adding arguments -xmx512M -xms512M should do the trick, since it increases the heap size.

Upvotes: 0

Related Questions