1

I implemented Producer/Consumer pattern with BlockingCollection for my experiment.

PerformanceCounter c = null;
void Main()
{
    var p  =System.Diagnostics.Process.GetCurrentProcess();
    c = new PerformanceCounter("Process", "Working Set - Private", p.ProcessName);

    (c.RawValue/1024).Dump("start");
    var blocking = new BlockingCollection<Hede>();
    var t = Task.Factory.StartNew(()=>{
        for (int i = 0; i < 10000; i++)
        {
            blocking.Add(new Hede{
            Field = string.Join("",Enumerable.Range(0,100).Select (e => Path.GetRandomFileName()))
            });
        }
        blocking.CompleteAdding();
    });

    var t2 = Task.Factory.StartNew(()=>{
        int x=0;
        foreach (var element in blocking.GetConsumingEnumerable())
        {
            if(x % 1000==0)
            {
                (c.RawValue/1024).Dump("now");
            }
            x+=1;   
        }
    });
    t.Wait();
    t2.Wait();
    (c.RawValue/1024).Dump("end");
}

My memory dump after running:

start
211908
now
211972
now
212208
now
212280
now
212596
now
212736
now
212712
now
212856
now
212840
now
212976
now
213036
end
213172

My memory is 211908kb before consuming, but it increased one by one while producing from other thread.

GC did not collect the produced objects from memory. Why my memory increment although implemented producer/consumer pattern?

Ernesto
  • 1,523
  • 1
  • 14
  • 32
oguzh4n
  • 682
  • 1
  • 10
  • 29
  • I don't see any Dispose or Using calls. I see you adding items to a collection and never removing them. I don't quite understand what the issue is or what you're trying to achieve. – Origin Jan 02 '12 at 15:34
  • 1
    I don't really understand what you are expecting to happen? – Tudor Jan 02 '12 at 15:34
  • 1
    Origin: it's producer/consumer pattern, I don't need remove item from list. GetConsumingEnumerable() removes item for me – oguzh4n Jan 02 '12 at 15:40
  • 1
    Your results don't indicate a leak or anything. Lots of items consume memory and your difference are tiny. Run it for 2 days and it'll probably stabilize. – H H Jan 02 '12 at 15:47
  • Your title does not match your question! GC does collect unused object **when it decides so**. Since there are no GC.Collect calls you are measuring simply if you program creates objects. – Alexei Levenkov Jan 02 '12 at 18:59
  • Maybe you should try GetTotalMemmory method (http://msdn.microsoft.com/en-us/library/system.gc.gettotalmemory.aspx) instead of that counter, as it seems it's not that much of an accurate unit of measure like described here: http://stackoverflow.com/questions/2611141/calculate-private-working-set-memory-using-c-sharp – Ernesto Jan 02 '12 at 20:39

2 Answers2

6

The ConcurrentQueue<T> that is used by default by BlockingCollection<T> contains a memory leak in .Net 4.0. I'm not sure this is the problem you're observing, but it could be.

It should be fixed in the upcoming .Net 4.5.

svick
  • 236,525
  • 50
  • 385
  • 514
  • While it is true about the ConcurrentQueue, the 'leak' will never be more than the unused part of the Queue. You would normally use an upper bound, like `new BlockingCollection(100)` and then you could run this forever. – H H Jan 02 '12 at 16:02
  • @Henk, yeah, buth with the code in the question, the queue has no bound, and it's quite possible that it will be first filled in completely and only then will the items be consumed. If that's the case, then all of the items will leak. – svick Jan 02 '12 at 16:11
0

The Garbage Collector doesn't automatically free memory all the time. You can force garbage collection by calling GC.Collect(); after disposing of unused objects.

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
  • i tried `GC.Collect();GC.WaitForPendingFinalizers();GC.Collect();` after `t2.Wait();` line, does not matter – oguzh4n Jan 02 '12 at 15:43