Tag: concurrent collections

ConcurrentBag导致内存泄漏吗?

可能重复: ConcurrentBag中可能的内存泄漏? 我的应用程序中有史诗内存泄漏。 从未收集过我在其中一种方法中添加到本地concurrentBag集合中的所有数据。 这个简单的代码演示了我如何使用它: void Main() { var l = new List(){1,2,3,4}; Func(l); l.Clear(); l=null; } void Func(List list) { var bag = new ConcurrentBag(); Parallel.ForEach(list, k=> bag.Add(++k)); list.Clear(); foreach(int i in bag) //I know, I doing it wrong. { list.Add(i); } } 我的期望:将创建包并在方法“Func”处置。 我所看到的:bag永远不会被处置,保存在Parallel.ForEach中创建的所有线程,保存我添加的所有数据。 =( 好的,当我将它添加到列表中时,我可以使用“TryTake”从包中删除项目。 但空袋仍然留在记忆中。 现在我通过使用List而不是ConcurrentBag来解决问题。 但是因为我在记忆中看到这个,所以我睡不着觉。 抱歉我的eng =) UPDATE 我改变了我的方法“Func”: […]

ConcurrentDictionary是否是线程安全的,我可以将它用于静态缓存?

基本上,如果我想做以下事情: public class SomeClass { private static ConcurrentDictionary Cache { get; set; } } 这是否让我避免在整个地方使用lock ?

在ConcurrentDictionary AddOrUpdate中为更新部分添加的内容

我试图使用Dictionary重新编写一些代码来使用ConcurrentDictionary。 我已经回顾了一些示例,但我仍然无法实现AddOrUpdate函数。 这是原始代码: dynamic a = HttpContext; Dictionary userDic = this.HttpContext.Application[“UserSessionList”] as Dictionary; if (userDic != null) { if (useDic.ContainsKey(authUser.UserId)) { userDic.Remove(authUser.UserId); } } else { userDic = new Dictionary(); } userDic.Add(authUser.UserId, a.Session.SessionID.ToString()); this.HttpContext.Application[“UserDic”] = userDic; 我不知道要为更新部分添加什么: userDic.AddOrUpdate(authUser.UserId, a.Session.SessionID.ToString(), /*** what to add here? ***/); 任何指针将不胜感激。