Memory leak with ConcurrentQueue

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

提交回复
热议问题