• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 544
  • Last Modified:

C# Memory leak with ConcurrentQueue

0 down vote favorite
      

i have memory leak when using ConcurrentQueue :
requestObject request = xxx;

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

request.Key = obj.Key;

obj.AddRequest(request);

_queue.TryAdd(obj.Key, obj);

Open in new window


In the "Exited" callback, i dispose the resource :
void LiveSphere_Exited(string key)
{
   Item instance;

   _queue.TryRemove(key, out instance);

Task.Factory.StartNew(() =>
{
    var wait = new SpinWait();
                while (instance.MessageCount > 0)
                {
                    wait.SpinOnce();
                }
            }).ContinueWith((t) =>
            {
                if (instance != null)
                {
                    //Cleanup resources
                    instance.MessageReceived -= obj_MessageReceived;
                    instance.Exited -= obj_Exited;
                    instance.Dispose();
                    instance = null;
                }
            });

Open in new window

When i profile the code, i still have a root referenced "Item" object but i don't know where i can dispose..., The exited method is triggered and the _queue has removed the "Item" object from the queue.

When i read documentation, the concurrentqueue copy the reference into the queue.

Can you help me to find out where the memory leak is?

Thank you,

Kind Regards.
0
Dnx_7
Asked:
Dnx_7
  • 2
1 Solution
 
hpdvs2Commented:
Are you sure nothing else is holding on to the Item object?  I can see you grabbed a reference from your code, and called the dispose method, however, something else may still have a reference to it.  which means it will never get collected.

You referred to this:
i still have a root referenced "Item" object
I'm wondering if that means you are looking at another reference still tied to the object.  For instance, are you removing an item from a tree, but still leaving parent/child references intact, or something similar?
0
 
Dnx_7Author Commented:
Hi,

Thank you for your quick response.

No where else, the "Item" object is used, the "Item" object is declared within a method, add to a concurrentqueue and that's it.

The item triggered the "Exited" when the item can be disposed, what i do in the Exited Callback method.

i still have a rooted ref, i double check inside the "Item" object but everything is cleared about resources
0
 
Dnx_7Author Commented:
i found myself the solution
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now