2011-06-28 25 views
3

次の単純なStAXコードを使用して、XMLのすべてのタグを反復処理しています。 input.xmlに> 100メガバイトStAXのメモリ不足エラー

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 Parserと同じエラーが発生しています。

答えて

1

ヒープサイズを定義します

-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 
+0

Eclipse HeliosをIDEとして使用していますが、eclipse.iniなどでこれらの変更を行う必要がありますか?また、mとMの違いは何ですか?それはmBとMBですか? – Anand

+1

MはMBを表します。 Eclipse IDEにVM引数を追加する必要があります( '実行コンフィギュレーション - >実行するプログラムの選択 - >引数 - > VM引数の追加') –

+0

ありがとうございました。しかし、これは、今後1GBを超えるサイズのファイルを使用すると、コードが再びメモリ不足になることはありません。はいの場合、なぜですか? – Anand

1

-Xmxパラメータを使用して、VmのMaxHeapサイズを大きくします。 JVMの実行中に

java -Xmx512m .... 
+0

これで問題ありませんが、XMLサイズが指定された最大サイズを上回る場合は、再び失敗します。そのようなケースをすべて処理するように、堅牢なソリューション/コードの微調整はありませんか? – Anand

+0

DOMはファイル全体をメモリにロードする必要があります.SAXはストリーム処理を行い、別の制限(たぶんヒットします)を増やすだけです。したがって、はるかに大きなファイルであっても処理することが可能です。 – stacker

+0

メニュー "Run" - > "Run Configurations"このダイアログでは、 "引数"タブの "VM引数"で指定できます。 – stacker

0

: は伝統的に、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の代わりに使用できるならば。

しかし、JVM64でファイルサイズ2,6GBのコードを試しました。問題なし。だから私はその問題はファイルのサイズではなく、データのためかもしれないと思います。