views:

221

answers:

1

Hi all,

Basically, I simply want to do many searches on a given lucene index.

Therefore, I made a class Data with final 'analyzer', 'reader', 'searcher' and 'parser' fields, (all properly initialized in the constructor). The class also provides a 'search' method to search the index. This is all shown in the code below.

The problem is however that memory gradually becomes filled after many calls to 'search' (with different queries). I did not check what happens when always the same query is used. I looked around already for possible answers, and it seems to be best practice to keep the searcher etc open across different searches, so I guess that is not the problem. Any other ideas?

Thanks, Joachim.

(example) code:

public class Data {

private final Analyzer analyzer;
private final IndexReader reader;
private final IndexSearcher searcher;   
private final QueryParser parser;

public Data(String indexPath, Analyzer analyzer) throws IOException {
    this.analyzer = analyzer;
    Directory directory =  FSDirectory.open(indexPath);
    reader = new FilterIndexReader(IndexReader.open(directory, true));
            directory.close();
    searcher = new IndexSearcher(reader);
    parser =  new QueryParser(Version.LUCENE_CURRENT, 
                                           FieldName.CONTENT, analyzer);
}    

public TopDocs search(String line, Integer maxHits) throws ... { 
    Query query = parser.parse(QueryParser.escape(line));
    return searcher.search(query, maxHits);
}

}

+1  A: 

The garbage collector will probably free up memory at some point before the app runs out of memory.

I don't see why instances would be held in memory by the search method, but you could use a profiling tool such as JProfiler to confirm.

Bruno Rothgiesser
Well, even explicitly calling the gc did not keep the program from eventually eating all memory. I did find out in the mean time that the problem only occurs with openjdk. I ran the same code with SUN's jdk and it is OK...
Joachim De Beule
No, I didn't mean that you should explicitly call the gc. I thought that it was probably not an actual memory leak in your code and that the gc would eventually free up memory automatically.
Bruno Rothgiesser
Actually it is not freeing up the memory.
Jenea