3

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?

1
  • 1
    The XStream constructor can have a parameter with the type of xml parser to use specified, i think, maybe this helps you (not load the whole xml in memory at once): xstream.codehaus.org/tutorial.html Commented Apr 10, 2014 at 10:44

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.