为什么GC不收集未使用的对象?

我为我的实验用BlockingCollection实现了Producer / Consumer模式。

PerformanceCounter c = null; void Main() { var p =System.Diagnostics.Process.GetCurrentProcess(); c = new PerformanceCounter("Process", "Working Set - Private", p.ProcessName); (c.RawValue/1024).Dump("start"); var blocking = new BlockingCollection(); var t = Task.Factory.StartNew(()=>{ for (int i = 0; i  Path.GetRandomFileName())) }); } blocking.CompleteAdding(); }); var t2 = Task.Factory.StartNew(()=>{ int x=0; foreach (var element in blocking.GetConsumingEnumerable()) { if(x % 1000==0) { (c.RawValue/1024).Dump("now"); } x+=1; } }); t.Wait(); t2.Wait(); (c.RawValue/1024).Dump("end"); } 

运行后我的内存转储:

 start 211908 now 211972 now 212208 now 212280 now 212596 now 212736 now 212712 now 212856 now 212840 now 212976 now 213036 end 213172 

在消费之前我的记忆是211908kb,但是从其他线程产生时它一个接一个地增加。

GC没有从内存中收集生成的对象。 尽管实施了生产者/消费者模式,为什

BlockingCollection默认使用的ConcurrentQueue 包含.Net 4.0中的内存泄漏 。 我不确定这是你正在观察的问题,但它可能是。

它应该在即将发布的.Net 4.5中修复。

垃圾收集器不会自动释放内存。 您可以通过调用GC.Collect();强制垃圾回收GC.Collect(); 处理掉未使用的物体后。