我想知道字典项的大小(单位为字节)。我该如何做呢?有没有预定义的方法可以使用?我看到类似的问题被回答说要使用GC.TotalMemory方法来获取差异,但是在这里我无法使用Gc.KeepAlive对象。请给出建议。在使用字典之前和之后计算差异的问题不可行,因为总内存不仅包括该字典的内存,还包括其他几个线程的内存。请提供另一种方法。
我想知道字典项的大小(单位为字节)。我该如何做呢?有没有预定义的方法可以使用?我看到类似的问题被回答说要使用GC.TotalMemory方法来获取差异,但是在这里我无法使用Gc.KeepAlive对象。请给出建议。在使用字典之前和之后计算差异的问题不可行,因为总内存不仅包括该字典的内存,还包括其他几个线程的内存。请提供另一种方法。
long StopBytes = 0;
// Declare your dictionnary
Dictionary<string, int> myDictionary;
// Get total memory before create your dictionnary
long StartBytes = System.GC.GetTotalMemory(true);
// Initialize your dictionnary
myDictionary = new Dictionary<string, int>();
// Get total memory after create your dictionnary
StopBytes = System.GC.GetTotalMemory(true);
// This ensure a reference to object keeps object in memory
GC.KeepAlive(myFoo);
// Calcul the difference , and that all :-)
MessageBox.Show("Size is " + ((long)(StopBytes - StartBytes)).ToString());
更多信息请点击这里 -> http://blogs.msdn.com/b/mab/archive/2006/04/24/582666.aspx