Solved

C# Memory leak with ConcurrentQueue

Posted on 2012-12-26
3
401 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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