C# 我应该在不再使用时显式清除非全局ConcurrentDictionary,还是GC检测到它不再使用?
我应该在不再使用时显式清除非全局ConcurrentDictionary,还是GC检测到它不再使用 这有点相关:C# 我应该在不再使用时显式清除非全局ConcurrentDictionary,还是GC检测到它不再使用?,c#,.net,memory-management,memory-leaks,f#,C#,.net,Memory Management,Memory Leaks,F#,我应该在不再使用时显式清除非全局ConcurrentDictionary,还是GC检测到它不再使用 这有点相关: 编辑: 我问的原因是,我有以下代码(此处简化): let myMethod()= 异步的{ 让myEventList=ConcurrentDictionary() 使用o=myEvent |>Observable.subscribe(有趣的e-> myEventList.GetOrAdd(e,true)|>忽略) let!res=doAsyncThingsCausingEvents
编辑: 我问的原因是,我有以下代码(此处简化):
let myMethod()=
异步的{
让myEventList=ConcurrentDictionary()
使用o=myEvent |>Observable.subscribe(有趣的e->
myEventList.GetOrAdd(e,true)|>忽略)
let!res=doAsyncThingsCausingEvents()
myEventList |>Seq.iter(fun(e,|)->Console.WriteLine“We had”+e.ToString())
物件
}
…事实上,当在多个线程中多次并行调用时,它会泄漏内存。一个有趣的问题。
ConcurrentDictionary
不可IDisposable
,因此我希望它能像任何其他对象一样工作。i、 e.当不再引用时,应符合收集条件,包括没有其他引用的所有物品
通过分配ConcurrentDictionary、清空引用和捕获内存快照进行快速测试表明,情况确实如此