Solved

C# Memory leak with ConcurrentQueue

Posted on 2012-12-26
3
311 Views
Last Modified: 2015-05-29
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
Comment
Question by:Dnx_7
  • 2
3 Comments
 
LVL 8

Expert Comment

by:hpdvs2
ID: 38721157
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
 
LVL 2

Accepted Solution

by:
Dnx_7 earned 0 total points
ID: 38721212
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
 
LVL 2

Author Closing Comment

by:Dnx_7
ID: 40802069
i found myself the solution
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now