我正在使用Lucene 2.4.0和JVM(JDK 1.6.0_07).OutOfMemoryError: Java heap space在尝试索引大型文本文件时,我一直在接收.
示例1:索引5 MB文本文件的内存不足,最大64 MB.堆大小.所以我增加了最大值.堆大小为512 MB.这适用于5 MB的文本文件,但Lucene仍然使用84 MB的堆空间来执行此操作.为什么这么多?
FreqProxTermsWriterPerField根据JConsole和Eclipse Ganymede的TPTP Memory Profiling插件,该类似乎是迄今为止最大的内存消费者.
示例2:索引62 MB文本文件的内存不足,最大512 MB.堆大小.增加最大值 堆大小为1024 MB,但Lucene在执行此操作时使用了826 MB的堆空间.似乎还有太多的内存被用来做这件事.我确定较大的文件会导致错误,因为它似乎相关.
我在拥有2 GB RAM的Windows XP SP2平台上.那么索引大文件的最佳做法是什么?这是我正在使用的代码段:
// Index the content of a text file.
private Boolean saveTXTFile(File textFile, Document textDocument) throws MyException {
try {
Boolean isFile = textFile.isFile();
Boolean hasTextExtension = textFile.getName().endsWith(".txt");
if (isFile && hasTextExtension) {
System.out.println("File " + textFile.getCanonicalPath() + " is being indexed");
Reader textFileReader = new FileReader(textFile);
if (textDocument == null)
textDocument = new Document();
textDocument.add(new Field("content", textFileReader));
indexWriter.addDocument(textDocument); // BREAKS HERE!!!!
}
} catch (FileNotFoundException fnfe) {
System.out.println(fnfe.getMessage());
return false;
} catch (CorruptIndexException cie) {
throw new MyException("The index has become corrupt.");
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
分析是确定如此大的内存消耗的唯一方法。
另外,在您的代码中,您没有关闭Filehandlers,Indexreaders,Inderwriters,这可能是 OOM 的罪魁祸首,