0

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
4

1 回答 1

1

我可能是错的,但是我认为当您调用时tmp.RemoveFirst(),尺寸tmp.Count会变小,而尺寸会i不断增长。所以,你只删除了一半的Dictionary. 要么不增加i要么 将tmp.Count放入变量然后在循环条件中使用它。考虑到您最后一次 Force Collect 的大小约为一半,这看起来可能是您的问题。

于 2012-05-24T00:50:34.707 回答