Memory leak with ConcurrentQueue

后端 未结 2 1438
情歌与酒
情歌与酒 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.

    0 讨论(0)
  • 2021-01-12 04:26

    I have looked implementation of the Concurrent Queue. There are cases when the queue will hold references to the object after Dequeue() was called.

    Concurrent Queue uses Segments to store data. There it is a part of the TryRemove method of the segment:

    // If there is no other thread taking snapshot (GetEnumerator(), ToList(), etc), reset the deleted entry to null.
    // It is ok if after this conditional check m_numSnapshotTakers becomes > 0, because new snapshots won't include 
    // the deleted entry at m_array[lowLocal]. 
    if (m_source.m_numSnapshotTakers <= 0)
    {
        m_array[lowLocal] = default(T); //release the reference to the object. 
    } 
    

    So when you have a different thread that enumerates the queue at the same time you dequeue an object references to the object will not be set to null.

    0 讨论(0)
提交回复
热议问题