Solved

C# Memory leak with ConcurrentQueue

Posted on 2012-12-26
3
379 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
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 …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 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