GC.GetTotalMemory(false)和GC.GetTotalMemory(true)之间的区别是什么

som*_*raj 3 .net

有人可以告诉我GC.GetTotalMemory(false)和GC.GetTotalMemory(true)之间的区别;

我有一个小程序,当我比较结果时,第一个循环为GC.GetTotalMemory(true)给出了put put <循环计数0 Diff = 32>; 和<循环计数0 Diff = 0>表示GC.GetTotalMemory(false); 但不应该是其他的吗?

微妙的其余循环打印出一些数字,这两种情况都不同.这个数字表示什么.为什么它会随着循环的增加而改变.

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace test
{   
   struct Address
   {
       public string Street;
   }
   class Details
   {
      public string Name ;
      public Address address = new Address();

   }
   class emp :IDisposable
   {
       public Details objb = new Details();       
       bool disposed = false;
       #region IDisposable Members
       public void Dispose()
       {
           Disposing(true);
       }
       void Disposing(bool disposing)
       {
           if (!disposed)
               disposed = disposing;
           objb = null;           
           GC.SuppressFinalize(this);
       }

       #endregion
   }


    class Program
    {       
        static void Main(string[] args)
        {        
           long size1 = GC.GetTotalMemory(false);
           emp empobj = null;          
           for (int i = 0; i < 200;i++ )
           {
              // using (empobj = new emp()) //------- (1)
               {
                   empobj = new emp(); //------- (2)
                   empobj.objb.Name = "ssssssssssssssssss";
                   empobj.objb.address.Street = "asdfasdfasdfasdf";
               }

              long size2 = GC.GetTotalMemory(false);             
              Console.WriteLine( "loop count " +i + "  Diff = " +(size2-size1));

           }
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

Dro*_*per 5

该参数定义是否在运行之前等待完整的垃圾收集.

请参阅MSDN:参数

forceFullCollection
类型:System.Boolean
true表示此方法可以在返回之前等待垃圾收集; 否则,错误.

diff仍为0的原因可能是因为GC已经发生,即使你传递了false.