Edit: Please ignore this question, this is a PBCAK programmer error.
Part 1
How can I correct the memory leak in this code?
Part 2
If the solution includes adding Dispose
to SummaryEntity
, how should I dispose of the object if I have a second list (summary-of-summaries) that references it? Assume I want to keep the 10 most recent SummaryEntities
in the summary-of-summaries
Code
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
{
}
}
}
}
Output
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