Ошибка нехватки памяти в StAX - PullRequest
3 голосов
/ 28 июня 2011

Я использую следующий простой код StAX для перебора всех тегов в XML. Размер input.xml> 100 МБ

XMLInputFactory xif = XMLInputFactory.newInstance();
        FileInputStream in = new FileInputStream("input.xml");
        XMLStreamReader xsr = XMLInputFactory.newInstance().createXMLStreamReader(in);

        xsr.next();
        while (xsr.hasNext()) {

            xsr.next();
            if(xsr.isStartElement() || xsr.isEndElement())
                 System.out.println(xsr.getLocalName());            
            }
        }

Я получаю эту ошибку:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

Пожалуйста, скажите мне, как обойти это. Я читал, что StAX хорошо обрабатывает огромные XML-файлы, но я получаю ту же ошибку, что и DOM Parsers.

Ответы [ 3 ]

1 голос
/ 28 июня 2011

Определение размера кучи во время работы JVM

-Xms    initial java heap size
-Xmx    maximum java heap size
-Xmn    the size of the heap for the young generation

Пример:

bin/java.exe -Xmn100M -Xms500M -Xmx500M
1 голос
/ 28 июня 2011

Увеличьте размер MaxHeap вашего Vm с помощью параметра -Xmx.

java -Xmx512m ....
0 голосов
/ 28 июня 2011

Из Википедии: Традиционно XML API являются либо:

tree based - the entire document is read into memory as a tree structure for random 
access by the calling application
event based - the application registers to receive events as entities are encountered 
within the source document.

StAX was designed as a median between these two opposites. In the StAX metaphor,
the  programmatic  entry point is a cursor that represents a point within the 
document. The application moves the cursor forward - 'pulling' the information from 
the parser as it needs. This is different from an event based API - such as SAX - 
which 'pushes' data to the application - requiring the application to maintain state 
between events as necessary to keep track of location within the document.

Так что для 100M и более - я предпочитаю SAX - если возможно, используйте вместо StAX.

Но я попробовал ваш кодс размером файла 2,6GB на JVM64.Без проблем.Поэтому я полагаю, что проблема не в размере файла, а в том, что данные могут быть.

...