有人告訴我GC.GetTotalMemory(false)和GC.GetTotalMemory(true)之間的區別。GC.GetTotalMemory(false)和GC.GetTotalMemory(true)之間的區別是什麼
我有一個小程序,當我比較結果時,第一個循環 給出了放置放<循環計數0 Diff = 32> for GC.GetTotalMemory(true);對於GC.GetTotalMemory(false),爲 和<循環計數0 Diff = 0>; 但它不應該是另一回事?
其餘的循環會打印一些數字,這兩種情況都不相同。這個數字表示什麼?爲什麼它隨着循環的增加而變化。
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));
}
}
}
}
完整垃圾回收... – Peter 2010-04-06 06:13:03
在當我通過真實的情況下,我預期得到差異= 0,因爲方法等待垃圾收集。 但是當我通過false時,我得到了diff = 0,這讓我感到困惑 – somaraj 2010-04-06 06:28:43