Share via


Memory allocated for c# dictionary elements is not getting released when removing instated it always increase

Question

Monday, September 29, 2014 12:00 PM

Hi All,

I have an situation here. I am using a Dictionary<int, Object>(). To which I am continiously Adding and Removing the elements but when I see the memory consumption it is always increasing. I also tried reallocating the memory for the same dictionary as follows:

_DT=new Dictionary<int, object>(_DT);

but this also doesn't help. 

Is there an way to release the memory for dictionary when Adding and removing element?

Thanks and Regards,

Lucas

All replies (6)

Monday, September 29, 2014 1:09 PM ✅Answered | 1 vote

You need to delete the object before deleting the row from the dictionary if you actually want to delete the object.  I guess the dictionary could get fragmented when values (objects) are not all in the same memory area.  The objects are links and if the size of the link is different then the dictionary can get fragmented like any other object.

jdweng


Wednesday, October 1, 2014 7:28 PM ✅Answered | 1 vote

The garbage collector will only clean up an object if there are no references being held that point to it.  Your dictionary entry is only one instance of a reference, but do you have others?  Perhaps you're holding on to the object in another class?

Also, garbage collection runs when there is low CPU demand, or memory pressure insists upon it. 


Wednesday, October 1, 2014 7:57 PM ✅Answered

Thanks for the reply..

But is there any way to release the memory for such elements cause if the memory is not getting released then the memory footprints are tremendous and becomes one of the bottelnecks for performance.

Lucas

Yes, there is a way to force a garbage collection, which naturally takes time.  However, the CLR is pretty good at managing memory.  Do not expect for memory to be immediately freed as you step through code, or hit a breakpoint.  The garbage collector will do its' thing whenever it feels it is needed.  As long as you're not running out of memory, I wouldn't worry about it.  One objective of managed code is to free the programmer from the mundane concerns of managing memory usage.

However, follow the above insight and guidance.  As long as there is a something that is referencing the object, then the GC will not collect it.  Beware of referencing objects through event handlers.  While you may clear out all of your variables that reference an object, if any of its' event delegates are referencing "live" objects, then the object will not be collected.

Hope this helps.

Rudy   =8^D

Mark the best replies as answers. "Fooling computers since 1971."

http://thesharpercoder.com/


Monday, September 29, 2014 12:12 PM

The value is a link to the object not a copy of the object.  When you delete the object from the dictionary it removes the link to the object, but doesn't delete the object.

jdweng


Monday, September 29, 2014 12:26 PM | 1 vote

Lucas, how do you measure memory consumption?

C# Garbage Collection is not releasing memory just after losing all references to object. Please refer to Garbage Collection on MSDN.


Monday, September 29, 2014 12:41 PM

Thanks for the reply..

But is there any way to release the memory for such elements cause if the memory is not getting released then the memory footprints are tremendous and becomes one of the bottelnecks for performance.

Lucas