編集:この質問は無視してください。これはPBCAKプログラマーのエラーです。
パート1
このコードのメモリリークを修正するにはどうすればよいですか?
パート2
ソリューションにDispose
への追加が含まれてSummaryEntity
いる場合、オブジェクトを参照する2番目のリスト(要約の要約)がある場合、オブジェクトをどのように破棄する必要がありますか?SummaryEntities
要約の要約で最新の10個を保持したいとします
コード
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