Lucene.Net 使用 Sort 排序会出现内存泄漏问题,直到 1.9.1-001-13Jul06 版本,该问题依然存在。
测试代码
解决方法
转自:
blog.dotlucene.net
解决lucene 1.* 使用排序后内存溢出问题
1. 在 FieldSortedHitQueue (Search\FieldSortedHitQueue.cs) 类中增加如下方法 。
2. 在 FieldCacheImpl (Search\FieldCacheImpl.cs) 类中增加如下方法。
3. 修改 IndexSearcher (Search\IndexSearcher.cs) Close() 方法,增加两行代码。
4. 为 FieldCache Interface (Search\FieldCache.cs) 增加一个 Close 方法。
测试代码
private void MemoryLeak()
{
Directory directory = new RAMDirectory();
Analyzer analyzer = new StandardAnalyzer();
IndexWriter writer = new IndexWriter(directory, analyzer, true);
for (int i = 1; i <= 1000; i++)
{
Document document = new Document();
document.Add(Field.Keyword("field1", i.ToString()));
writer.AddDocument(document);
}
writer.Close();
int x = 0;
while (true)
{
IndexSearcher searcher = new IndexSearcher(directory);
//Sort sort = new Sort("field1"); // remove this line to avoid the memory leak
Sort sort = new Sort(new SortField[] { new SortField("field1", SortField.INT, false), SortField.FIELD_DOC });
Query query = QueryParser.Parse("1*", "field1", analyzer);
Hits hits = searcher.Search(query, sort);
searcher.Close();
Console.WriteLine("{1} - {0} MB", Process.GetCurrentProcess().WorkingSet64 / 1024 / 1024, ++x);
}
}
{
Directory directory = new RAMDirectory();
Analyzer analyzer = new StandardAnalyzer();
IndexWriter writer = new IndexWriter(directory, analyzer, true);
for (int i = 1; i <= 1000; i++)
{
Document document = new Document();
document.Add(Field.Keyword("field1", i.ToString()));
writer.AddDocument(document);
}
writer.Close();
int x = 0;
while (true)
{
IndexSearcher searcher = new IndexSearcher(directory);
//Sort sort = new Sort("field1"); // remove this line to avoid the memory leak
Sort sort = new Sort(new SortField[] { new SortField("field1", SortField.INT, false), SortField.FIELD_DOC });
Query query = QueryParser.Parse("1*", "field1", analyzer);
Hits hits = searcher.Search(query, sort);
searcher.Close();
Console.WriteLine("{1} - {0} MB", Process.GetCurrentProcess().WorkingSet64 / 1024 / 1024, ++x);
}
}
解决方法
转自:
blog.dotlucene.net
解决lucene 1.* 使用排序后内存溢出问题
1. 在 FieldSortedHitQueue (Search\FieldSortedHitQueue.cs) 类中增加如下方法 。
internal static void Close(IndexReader reader)
{
lock (Comparators.SyncRoot)
{
System.Collections.Hashtable readerCache = (System.Collections.Hashtable)Comparators[reader];
if (readerCache != null)
{
readerCache.Clear();
}
Comparators.Remove(reader);
}
}
{
lock (Comparators.SyncRoot)
{
System.Collections.Hashtable readerCache = (System.Collections.Hashtable)Comparators[reader];
if (readerCache != null)
{
readerCache.Clear();
}
Comparators.Remove(reader);
}
}
2. 在 FieldCacheImpl (Search\FieldCacheImpl.cs) 类中增加如下方法。
public virtual void Close(IndexReader reader)
{
lock (this)
{
System.Collections.Hashtable readerCache = (System.Collections.Hashtable)cache[reader];
if (readerCache != null)
{
readerCache.Clear();
readerCache = null;
}
cache.Remove(reader);
}
}
{
lock (this)
{
System.Collections.Hashtable readerCache = (System.Collections.Hashtable)cache[reader];
if (readerCache != null)
{
readerCache.Clear();
readerCache = null;
}
cache.Remove(reader);
}
}
3. 修改 IndexSearcher (Search\IndexSearcher.cs) Close() 方法,增加两行代码。
public override void Close()
{
FieldSortedHitQueue.Close(reader);
Lucene.Net.Search.FieldCache_Fields.DEFAULT.Close(reader);
if (closeReader)
reader.Close();
}
{
FieldSortedHitQueue.Close(reader);
Lucene.Net.Search.FieldCache_Fields.DEFAULT.Close(reader);
if (closeReader)
reader.Close();
}
4. 为 FieldCache Interface (Search\FieldCache.cs) 增加一个 Close 方法。
public interface FieldCache
{
//...
void Close(IndexReader reader);
}
{
//...
void Close(IndexReader reader);
}