不再使用时,我应该明确清除非全局 ConcurrentDictionary,还是 GC 检测到它不再使用?

Tuo*_*nen 5 .net c# f# memory-leaks memory-management

不再使用时,我应该明确清除非全局 ConcurrentDictionary,还是 GC 检测到它不再使用?

这有点相关:ConcurrentBag 是内存泄漏的原因吗?


编辑:我问的原因是,我有以下代码(此处简化):

let myMethod() =
  async {
    let myEventList = ConcurrentDictionary<myEventParams, bool>()
    use o = myEvent |> Observable.subscribe(fun e ->
            myEventList.GetOrAdd(e, true) |> ignore)
    let! res = doAsyncThingsCausingEvents()
    myEventList |> Seq.iter(fun (e,_) -> Console.WriteLine "We had " + e.ToString())
    res
  }
Run Code Online (Sandbox Code Playgroud)

...实际上它在多个线程中多次并行调用时会泄漏内存。

Jon*_*asH 2

有趣的问题。不是,ConcurrentDictionary所以IDisposable我希望它能像任何其他对象一样工作。即当它不再被引用时,它应该有资格被收集,包括任何没有其他引用的拥有的项目。

通过分配 ConcurrentDictionary、清空引用并捕获内存快照进行快速测试表明情况确实如此。