Does .Net 4.5.1 ConcurrentDictionary TryRemove() method release allocated memory?

3.7k views Asked by At

I have a cache consisting of elements each of which contains two concurrent dictionary and I think they may be responsible for memory leak in my application. I'm frequently adding and removing stuff from this dictionary. Can someone provide me description of how memory allocation works for concurrent dictionary and what the best practice would be in my case? Thanks in advance!

2

There are 2 answers

1
Mitch Wheat On

Any .NET collection's remove method will not free the memory, it will simply remove the reference from the collection. If nothing else references those objects, the garbage collector will eventually clean them up.

A common cause of references being held are forgetting to unwire event handlers.

Several .NET Memory profilers such as MemProfiler, dotTrace and ANTS memory profiler have trial versions.

As @Scott Chamberlain points out, the System.Runtime.Caching Namespace contains types that let you implement caching in NET Framework applications.

1
JeffRSon On

If the elements you are removing make use of unmanaged memory (bitmaps etc come to mind) these elements should implement IDisposable and you should call .Dispose before the last reference goes out of scope.