title: ConcurrentQueue holds object's reference or value? "out of memory" exception
body: <p>Is objects enqueued to a ConcurrentQueue are copied to the queue or just their references?</p>
  
  <p>I don't understand any scenario.</p>
  
  <p>Explanation:</p>
  
  <p>I defined a ConcurrentQueue like this:</p>
  
  <pre><code>// BufferElement is a class I created
  private ConcurrentQueue&lt;BufferElement&gt; _bufferQueue;
  </code></pre>
  
  <p>I have a function which is called a lot of times and it's purpsoe is to enqueue an element to the queue:</p>
  
  <pre><code>private void EnqueueElementToBuffer(string data, int moreData)
  {
      // the bufferElement constructor is setting data and moreData to it's fields.
      BufferElement bufferElement = new BufferElement(data, moreData);
      bufferQueue.Enqueue(bufferElement);
  }
  </code></pre>
  
  <p>When I run this I get Out of Memory exception after a while. I thought it might be because the garbage collector does not collect the <code>bufferElement</code> because it is still has referenced in the <code>bufferQueue</code>, so I changed the function to this:</p>
  
  <pre><code>private void EnqueueElementToBuffer(string data, int moreData)
  {
      // _bufferElement is now a filed of the class
      _bufferElement.Data = data;
      _bufferElement.MoreData = moreData;
      bufferQueue.Enqueue(_bufferElement);
  }
  </code></pre>
  
  <p>And I didn't get the exception, and wasn't about to get one judging by the memory in the windows task manager.</p>
  
  <p>Now I thought that the problam was solved because when I enqueued objects to the queue only the reference to the objects is cpoied to the queue, but I was afraid that all the elements in the queue are referencing to the same object, so I checked another function I have in another thread which what it does is:</p>
  
  <pre><code>    // while bufferQueue is not empty do the following
      BufferElement bufferElement = null;
      bufferQueue.TryDequeue(out bufferElement);
  </code></pre>
  
  <p>And I checked the content of couple of elements and saw that their content was different! so if objects enqueued to the queue are copied by value why I got the Out of Memory exception at first?</p>