ogu*_*h4n 1 c# memory-management producer-consumer
我为我的实验用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<Hede>();
var t = Task.Factory.StartNew(()=>{
for (int i = 0; i < 10000; i++)
{
blocking.Add(new Hede{
Field = string.Join("",Enumerable.Range(0,100).Select (e => 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");
}
Run Code Online (Sandbox Code Playgroud)
运行后我的内存转储:
start
211908
now
211972
now
212208
now
212280
now
212596
now
212736
now
212712
now
212856
now
212840
now
212976
now
213036
end
213172
Run Code Online (Sandbox Code Playgroud)
我的记忆在消耗之前是211908kb,但是在从其他线程产生时它一个接一个地增加.
GC没有从内存中收集生成的对象.尽管实施了生产者/消费者模式,为什
该ConcurrentQueue<T>所使用的默认情况下通过BlockingCollection<T> 包含在.NET 4.0中内存泄漏.我不确定这是你正在观察的问题,但它可能是.
它应该在即将发布的.Net 4.5中修复.
| 归档时间: |
|
| 查看次数: |
1187 次 |
| 最近记录: |