编辑:请忽略这个问题,这是PBCAK程序员的错误。
第1部分
如何更正此代码中的内存泄漏?
第2部分
如果解决方案包括将Dispose
添加到SummaryEntity
,如果我有第二个引用它的列表(摘要摘要),我应该如何处置该对象?假设我想在摘要摘要
SummaryEntities
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
namespace TestDoesLinkedListLeak
{
public class SummaryEntity
{
public object PlaceHolder { get; set; }
/// <summary>
/// Note: This is a Field instead of a Property to support Interlockd methods
/// </summary>
public int ReferenceCount;
public int Value;
public int? PreviousValue { get; set; }
}
class FrequencyOfMatchedHash : Dictionary<int, SummaryEntity>
{
private object frequencyOfMatchedHashLock;
public void AddRecordHash(int hashCode, int value, object placeHolder)
{
SummaryEntity se = null;
try
{
se = new SummaryEntity();
se.ReferenceCount = 1;
se.PlaceHolder = placeHolder;
se.Value = value;
lock (this.frequencyOfMatchedHashLock)
{
if (this.ContainsKey(hashCode))
{
System.Threading.Interlocked.Add(ref this[hashCode].Value, value);
System.Threading.Interlocked.Increment(ref this[hashCode].ReferenceCount);
}
else
{
this.Add(hashCode, se);
}
}
//this.AddOrUpdate(hashCode, se ,
// (k, v) =>
// {
// System.Threading.Interlocked.Add(ref v.Value , value);
// System.Threading.Interlocked.Increment(ref v.ReferenceCount);
// return v;
// });
}
finally
{
//se.Dispose();
}
}
}
class LLDateNode //:IDisposable
{
public FrequencyOfMatchedHash FrequencyOfMatch { get; set; }
public DateTime Date { get; set; }
public override string ToString()
{
return Date + " Count:" + FrequencyOfMatch.Count;
}
}
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("start");
Console.WriteLine("Total Memory: {0} Forced Collect: {1}", GC.GetTotalMemory(false), GC.GetTotalMemory(true));
LinkedList<LLDateNode> tmp = new LinkedList<LLDateNode>();
for (int i = 0; i < 111111; i++)
{
// Console.Write(".");
tmp.AddFirst(new LLDateNode() { Date = DateTime.UtcNow, FrequencyOfMatch = new FrequencyOfMatchedHash() });
}
Console.WriteLine();
Console.WriteLine("Done create, now delete");
Console.WriteLine("Total Memory: {0} Forced Collect: {1}", GC.GetTotalMemory(false), GC.GetTotalMemory(true));
Console.WriteLine();
for (int i = 0; i < tmp.Count; i++)
{
//Console.Write("x");
tmp.RemoveFirst();
}
Console.WriteLine();
Console.WriteLine("End of program");
Console.WriteLine("Total Memory: {0} Forced Collect: {1}", GC.GetTotalMemory(false), GC.GetTotalMemory(true));
Console.ReadLine();
Console.ReadLine();
Console.ReadLine();
Console.ReadLine();
}
catch
{
}
}
}
}
输出
start
Total Memory: 186836 Forced Collect: 106656
Done create, now delete
Total Memory: 11222752 Forced Collect: 11217844
End of program
Total Memory: 11226036 Forced Collect: 5662320
答案 0 :(得分:0)
我可能错了,但我认为当你呼叫tmp.RemoveFirst()
时,tmp.Count
的大小会变小,而i
会继续增长。所以,你只删除Dictionary
的一半。要么不增加i
,要么将tmp.Count
放入变量,然后在循环条件中使用它。考虑到你最后一次力量收集的大小约为一半,这看起来可能是你的问题。