Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7748045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:40:58+00:00 2026-06-01T10:40:58+00:00

Is objects enqueued to a ConcurrentQueue are copied to the queue or just their

  • 0

Is objects enqueued to a ConcurrentQueue are copied to the queue or just their references?

I don’t understand any scenario.

Explanation:

I defined a ConcurrentQueue like this:

// BufferElement is a class I created
private ConcurrentQueue<BufferElement> _bufferQueue;

I have a function which is called a lot of times and it’s purpsoe is to enqueue an element to the queue:

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);
}

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 bufferElement because it is still has referenced in the bufferQueue, so I changed the function to this:

private void EnqueueElementToBuffer(string data, int moreData)
{
    // _bufferElement is now a filed of the class
    _bufferElement.Data = data;
    _bufferElement.MoreData = moreData;
    bufferQueue.Enqueue(_bufferElement);
}

And I didn’t get the exception, and wasn’t about to get one judging by the memory in the windows task manager.

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:

    // while bufferQueue is not empty do the following
    BufferElement bufferElement = null;
    bufferQueue.TryDequeue(out bufferElement);

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:40:59+00:00Added an answer on June 1, 2026 at 10:40 am

    When you call Enqueue only a copy of the reference is stored in the ConcurrentQueue<T>. But this reference is strongly held which means it does keep the actual referred to object in memory. The element won’t be eligible for collection until the reference is removed from the ConcurrentQueue<T>

    The reason you didn’t see an OutOfMemoryException when you switched to using a field is because you fundamentally changed the semantics

    • Original Code: Pushed N references to N elements into the queue hence it’s holding N elements in memory
    • Changed Code: Pushed N references to 1 element into the queue hence it’s holding 1 element in memory

    This significantly reduced the amount of memory the ConcurrentQueue<T> object graph was holding in memory and prevented the exception.

    It seems like the problem here is you are simply equeuing elements much faster than you are processing them. So long as this holds true you will eventually run out of memory in the application. Your code needs to be adjusted such that it won’t hit this condition.

    Note: This answer is written assuming BufferElement is a class and not a struct.

    Note2: As Servy points out in the comments you may want to consider switching to a BlockingCollection<T> as it has a few throttling capabilities which may help you.

    • http://msdn.microsoft.com/en-us/library/dd267312.aspx
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I queue functions in PHP? I need something that works just like
I have a scenario where we will be getting 10-15 different XML messages enqueued
I have a circumstance where I have enqueued a number of Action objects and
I have a situation where very rarely a Queue of Objects is dequeuing a
i have built a Producer Consumer queue wrapping a ConcurrentQueue of .net 4.0 with
Basically I have a static custom queue of objects I want to process. From
Objects in javascript throw me for a loop! In this set up... var obj
All objects used as keys in NS(Mutable)Dictionaries must support the NSCopying protocol, and those
Preferences objects present a way to store arbitrary data into Rally which can be
What objects are created initially by compilers(?) of javascript? I've been learning Io in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.