我一直在尝试找到内存泄漏。我的对象图如下:(--> 是引用)
一个静态全局应用程序对象(控制器)--> 一个算法启动对象--> 文本挖掘启动对象--> 文本挖掘算法对象(此对象执行搜索)。
文本挖掘启动对象将在单独的线程中启动文本挖掘算法对象的run()方法。
为了尝试解决这个问题,我编辑了代码,使文本挖掘启动对象将要搜索的文本分成几组,并为每组文本顺序初始化一个文本挖掘算法对象(因此,当一个文本挖掘算法对象完成时,将创建一个新的对象来搜索下一组文本)。在这里,我将前一个文本挖掘算法对象设置为null。但是,这并没有解决问题。
当我创建一个新的文本挖掘算法对象时,我必须给它一些参数。这些参数是从之前的文本挖掘算法对象的属性中获取的,然后我将该对象设置为null。这会防止文本挖掘算法对象被垃圾回收吗?
下面是文本挖掘算法启动程序创建新文本挖掘算法对象的代码:
private void RunSeveralAlgorithmObjects()
{
IEnumerable<ILexiconEntry> currentEntries = allLexiconEntries.GetGroup(intCurrentAlgorithmObject, intNumberOfAlgorithmObjectsToUse);
algorithm.LexiconEntries = currentEntries;
algorithm.Run();
intCurrentAlgorithmObject++;
for (int i = 0; i < intNumberOfAlgorithmObjectsToUse - 1; i++)
{
algorithm = CreateNewAlgorithmObject();
AddAlgorithmListeners();
algorithm.Run();
intCurrentAlgorithmObject++;
}
}
private TextMiningAlgorithm CreateNewAlgorithmObject()
{
TextMiningAlgorithm newAlg = new TextMiningAlgorithm();
newAlg.SortedTermStruct = algorithm.SortedTermStruct;
newAlg.PreprocessedSynonyms = algorithm.PreprocessedSynonyms;
newAlg.DistanceMeasure = algorithm.DistanceMeasure;
newAlg.HitComparerMethod = algorithm.HitComparerMethod;
newAlg.LexiconEntries = allLexiconEntries.GetGroup(intCurrentAlgorithmObject, intNumberOfAlgorithmObjectsToUse);
newAlg.MaxTermPercentageDeviation = algorithm.MaxTermPercentageDeviation;
newAlg.MaxWordPercentageDeviation = algorithm.MaxWordPercentageDeviation;
newAlg.MinWordsPercentageHit = algorithm.MinWordsPercentageHit;
newAlg.NumberOfThreads = algorithm.NumberOfThreads;
newAlg.PermutationType = algorithm.PermutationType;
newAlg.RemoveStopWords = algorithm.RemoveStopWords;
newAlg.RestrictPartialTextMatches = algorithm.RestrictPartialTextMatches;
newAlg.Soundex = algorithm.Soundex;
newAlg.Stemming = algorithm.Stemming;
newAlg.StopWords = algorithm.StopWords;
newAlg.Synonyms = algorithm.Synonyms;
newAlg.Terms = algorithm.Terms;
newAlg.UseSynonyms = algorithm.UseSynonyms;
algorithm = null;
return newAlg;
}
这里是运行整个搜索过程的线程的开始:
// Run the algorithm in it's own thread
Thread algorithmThread = new Thread(new ThreadStart
(RunSeveralAlgorithmObjects));
algorithmThread.Start();
这里有什么可以防止先前的文本挖掘算法对象被垃圾回收吗?
AddAlgorithmListeners
方法的代码会很有趣。原因是:事件 可能 导致对象无法释放,因为某个监听器对象仍然订阅了对象的事件。只要监听器没有被释放,被监听的对象就不能够被处理掉。 - stakx - no longer contributing