Take the 2-minute tour ×

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));
        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);
share|improve this question
I ran into a similar issue with Lucene.NET 2.9.4. I'm unsure if it was specific to that or came from Lucene, but if I did not instantiate the IndexSearcher in a using block, it would gradually leak memory. Sorry, I'm unsure what the equivalent to using or try/catch/finally is in Java, and I'm unsure if it would solve your problem. But perhaps it will start you down the right path! –  Josh Feb 28 '13 at 6:03
add comment

1 Answer

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.

share|improve this answer
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 Feb 24 '10 at 14:05
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. –  b.roth Feb 24 '10 at 14:28
Actually it is not freeing up the memory. –  Jenea Apr 23 '10 at 14:54
add comment

Your Answer


