lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andy33 <>
Subject Re: Lucene Memory Leak
Date Fri, 05 Sep 2008 15:43:56 GMT

If I don't keep the IndexSearcher as a Singleton and instead open and close a
new one each time, I have a large memory leak (probably due to the large
queries I am doing). After watching the memory a while, I still believe I
have a small memory leak even when the Directory, Analyzer, and
IndexSearcher are Singletons. My free memory slowly becomes smaller after
each query. Any ideas on what that may be due to?

Here's my updated code...

private synchronized Hits doQuery(String field, String queryStr, Sort
sortOrder, String indexDirectory) throws Exception
	Directory directory = null;
	Query query = null;
	QueryParser parser = null;
	    directory = DirectorySingleton.getInstance(indexDirectory);
	    ivIndexSearcher = (IndexSearcher)
	    //search the index
	    parser = new QueryParser(field, AnalyzerSingleton.getInstance());
	    query = parser.parse(queryStr);
	    return, sortOrder);
	    if(null != directory)
	    directory = null;
	    parser = null;
	    query = null;   

Example Singleton

public class SearcherSingleton
    private static volatile HashMap<Directory, Searcher> cvSearches = new
HashMap<Directory, Searcher>();
    protected SearcherSingleton()
    public static Searcher getInstance(Directory directory) throws
		    cvSearches.put(directory, new IndexSearcher(directory));
	return cvSearches.get(directory);

장용석 wrote:
> In fact, I think that the important reasons are Directory class and
> Analyzer
> class.
> If you don't want IndexSearcher class keep open for the entire life of a
> web
> application, you can do it.
> I think It will not cause memory leak problem.
> But, Directory and Analyzer classes can cause the problem if they new
> created by method call every time. I think...
> Only keep two classes Directory and Analyzer by singlton and do test. :)

View this message in context:
Sent from the Lucene - Java Users mailing list archive at

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message