我的代码使用了许多字典,我在从所有字典中释放内存时遇到了问题。当我使用我在这个答案中学到的东西进行调查时,我看到 GenericEqualityComparer 在那里,我怀疑这就是保持这个内存在使用中的原因。
谁能证实这一点,或者告诉我如何释放这段记忆?
代码
Console.WriteLine("{0,10}: Start Point", GC.GetTotalMemory(true));
List<string> t1 = new List<string>();
Console.WriteLine("{0,10}: <------- Create List", GC.GetTotalMemory(true));
t1 = null;
Console.WriteLine("{0,10}: <------- null List", GC.GetTotalMemory(true));
GC.Collect();
Console.WriteLine("{0,10}: After GC.Collect", GC.GetTotalMemory(true));
Dictionary<string, string> t2 = new Dictionary<string, string>();
Console.WriteLine("{0,10}: <------- Create Dict", GC.GetTotalMemory(true));
t2 = null;
Console.WriteLine("{0,10}: <------- null Dict", GC.GetTotalMemory(true));
GC.Collect();
Console.WriteLine("{0,10}: After GC.Collect", GC.GetTotalMemory(true));
Dictionary<string, string> t3 = new Dictionary<string, string>();
Console.WriteLine("{0,10}: <------- Create Dict", GC.GetTotalMemory(true));
t3 = null;
Console.WriteLine("{0,10}: <------- null Dict", GC.GetTotalMemory(true));
GC.Collect();
Console.WriteLine("{0,10}: After GC.Collect", GC.GetTotalMemory(true));
Dictionary<string, string> t4 = new Dictionary<string, string>();
Console.WriteLine("{0,10}: <------- Create Dict", GC.GetTotalMemory(true));
t4 = null;
Console.WriteLine("{0,10}: <------- null Dict", GC.GetTotalMemory(true));
GC.Collect();
Console.WriteLine("{0,10}: After GC.Collect", GC.GetTotalMemory(true));
Dictionary<string, string> t5 = new Dictionary<string, string>();
Console.WriteLine("{0,10}: <------- Create Dict", GC.GetTotalMemory(true));
t5 = null;
Console.WriteLine("{0,10}: <------- null Dict", GC.GetTotalMemory(true));
GC.Collect();
Console.WriteLine("{0,10}: After GC.Collect", GC.GetTotalMemory(true));
Dictionary<string, string> t6 = new Dictionary<string, string>();
Console.WriteLine("{0,10}: <------- Create Dict", GC.GetTotalMemory(true));
GC.KeepAlive(t6);
t6 = null;
Console.WriteLine("{0,10}: <------- null Dict", GC.GetTotalMemory(true));
GC.Collect();
Console.WriteLine("{0,10}: <------- End.", GC.GetTotalMemory(true));
注意:我有这个代码的两个版本。如在 SO 上建议的那样,使用 GC.Collect 进行一次编辑,但这没有任何好处。
没有 GC 收集的输出
95884: Start Point
97872: <------- Create List
97888: <------- null List
97952: <------- Create Dict
97968: <------- null Dict
98032: <------- Create Dict
98048: <------- null Dict
98112: <------- Create Dict
98128: <------- null Dict
98192: <------- Create Dict
98208: <------- null Dict
98272: <------- Create Dict
98288: <------- null Dict
使用 GC 收集输出 (根据建议)
96004: Start Point
97992: <------- Create List
98008: <------- null List
98024: After GC.Collect
98088: <------- Create Dict
98104: <------- null Dict
98120: After GC.Collect
98184: <------- Create Dict
98200: <------- null Dict
98216: After GC.Collect
98280: <------- Create Dict
98296: <------- null Dict
98312: After GC.Collect
98376: <------- Create Dict
98392: <------- null Dict
98408: After GC.Collect
98472: <------- Create Dict
98488: <------- null Dict
98504: <------- End.
发布模式下的输出 (根据建议)
96028: Start Point
98016: <------- Create List
98032: <------- null List
98048: After GC.Collect
98112: <------- Create Dict
98128: <------- null Dict
98144: After GC.Collect
98208: <------- Create Dict
98224: <------- null Dict
98240: After GC.Collect
98304: <------- Create Dict
98320: <------- null Dict
98336: After GC.Collect
98400: <------- Create Dict
98416: <------- null Dict
98432: After GC.Collect
98496: <------- Create Dict
98512: <------- null Dict
98528: <------- End.
在没有 GC 收集的情况下以发布模式输出 (根据建议)
96028: Start Point
98016: <------- Create List
98032: <------- null List
98096: <------- Create Dict
98112: <------- null Dict
98176: <------- Create Dict
98192: <------- null Dict
98256: <------- Create Dict
98272: <------- null Dict
98336: <------- Create Dict
98352: <------- null Dict
98416: <------- Create Dict
98432: <------- null Dict
98448: <------- End.