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)
java.nazif
  • 539
  • 1
  • 8
  • 17
  • 1
    Do you really need to read it into memory all at once? – Kayaman Apr 07 '16 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. – Arnaud Denoyelle Apr 07 '16 at 16:23
  • You can increase the heap size with the [-Xmx command-line option](http://docs.oracle.com/javase/8/docs/technotes/tools/windows/java.html). – Andy Thomas Apr 07 '16 at 16:24
  • Andy cannot solve when I change xmx or xms – java.nazif Apr 07 '16 at 16:24
  • Can you tell more about the use case? Maybe we can help you to find a solution which does not require to load the whole file in memory. – Arnaud Denoyelle Apr 07 '16 at 16:26
  • Arnaud I want read xml firstly I read file and write bytes ,after do it bytes.I convert byte array to string.But it give me above error . – java.nazif Apr 07 '16 at 16:27
  • Are you using 32-bit or 64-bit JRE? What value did you provide for -Xmx? [You'll need > 1.5GB more.] BTW, see also former question ["Increase heap size in Java"](http://stackoverflow.com/questions/1565388/increase-heap-size-in-java). – Andy Thomas Apr 07 '16 at 16:27
  • @skynafo I mean : *why* do you need to read this xml? what do you plan to do with the extracted data? i.e. if you plan to count some tags or find the value of one attribute, it can easily be done with a SAX parser but if you need to navigate in the document, it is likely that it will require a DOM parser. – Arnaud Denoyelle Apr 07 '16 at 16:32
  • Arnaud I know What you mean but all of data of xml is necessary for me I will send it to company with webservice – java.nazif Apr 07 '16 at 16:35
  • Can you send an unparsed stream of the original XML? – Andy Thomas Apr 07 '16 at 16:41
  • 1
    Why are you using StringBuilder? – Raedwald Apr 07 '16 at 17:07
  • Raedwald I used it but It can not do it . – java.nazif Apr 07 '16 at 17:09

1 Answers1

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.

KayV
  • 9,560
  • 8
  • 68
  • 111