0

I have approximate 1.5 gigabyte xml file and I want to read this large file. But I'm getting following error .

UPDATE : I want to read this xml after above processing assign a string

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Unknown Source)
    at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)
    at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source)
    at java.lang.AbstractStringBuilder.append(Unknown Source)
    at java.lang.StringBuilder.append(Unknown Source)
    at gercek.FileToArrayOfBytes.main(FileToArrayOfBytes.java:28)
12
  • 1
    Do you really need to read it into memory all at once? Commented Apr 7, 2016 at 16:22
  • 1
    if you can analyze your document in one pass, use a SAX Parser (recommended). If you cannot, increase the heap size with the corresponding jvm option. Commented Apr 7, 2016 at 16:23
  • You can increase the heap size with the -Xmx command-line option. Commented Apr 7, 2016 at 16:24
  • Andy cannot solve when I change xmx or xms Commented Apr 7, 2016 at 16:24
  • 1
    Why are you using StringBuilder? Commented Apr 7, 2016 at 17:07

1 Answer 1

3

The best way to read a large xml file is to use the SAX Parser. It is efficient in the sense that it does not load the complete file at once. And thus you can get rid of outofmemoryexception.

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

2 Comments

Karan Verma :) it is not answer but I will give you vote point :) But again I will look it.
Suggest using StAX, not SAX. It's so much easier to use.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.