.NET Garbage Collection behavior (with DataTable)
        Posted  
        
            by 
                gmac
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by gmac
        
        
        
        Published on 2011-01-12T19:40:18Z
        Indexed on 
            2011/01/12
            19:53 UTC
        
        
        Read the original article
        Hit count: 187
        
I am wonder why after creating a very simple DataTable and then setting it to null why Garbage Collection does not clear out all the memory used by that DataTable. Here is an example. The variable Before should be equal to Removed but it is not.
{
 long Before = 0, After = 0, Removed = 0, Collected = 0;
 Before = GC.GetTotalMemory(true);
 DataTable dt = GetSomeDataTableFromSql();
 After = GC.GetTotalMemory(true);
 dt = null;
 Removed = GC.GetTotalMemory(true);
 GC.Collect();
 Collected = GC.GetTotalMemory(true);
}
Gives the following results.
Before = 388116
After = 731248
Removed = 530176
Collected = 530176
        © Stack Overflow or respective owner