Explicitly freeing memory in c#

前端 未结 9 716
刺人心
刺人心 2020-12-01 01:12

I\'ve create a c# application which uses up 150mb of memory (private bytes), mainly due to a big dictionary:

Dictionary Txns = new Diction         


        
9条回答
  •  长情又很酷
    2020-12-01 02:15

    Ok, I have a theory here... Dictionary is a collection of KeyValuePair which is again a reference type.

    Your dictionary contains these keyValuePairs. When you say:

    Txns = null
    

    It frees the reference 'Txns' from those KeyValuePair collection. But still the actual memory of 150 Mb is being referenced by those KeyValuePair and they are in scope, thus not ready for garbage collection.

    But when you use the following:

    Txns.Clear();
    Txns = null;
    GC.Collect();
    

    Here, the clear method also frees the 150Mb of data from their respective KeyValuePair object references. Thus those objects were ready for garbage collection.

    Its just a wild guess I am making here. Comments are welcome :)

提交回复
热议问题