Memory leak with ConcurrentQueue

后端 未结 2 1437
情歌与酒
情歌与酒 2021-01-12 03:48

i have memory leak when using ConcurrentQueue :

requestObject request = xxx;

Item obj= new Item ();
obj.MessageReceived += obj_MessageReceived         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-12 04:13

    Unlike a standard .NET Queue, calling Dequeue() does not remove the reference to the object from the collection. While this behavior has changed from the 4.0 version to the 4.5 version (I have read this, but have not tested it) it is not a bug, but a conscious design decision made by the framework team as a part of designing a thread-safe, enumerable collection.

    This article has more information, including a workaround by using StrongBox to wrap the objects that go into the ConcurrentQueue. That should be a suitable work-around until you can move to the 4.5 framework.

提交回复
热议问题