內部SortedDictionary<TKey, TValue>
使用TreeSet<KeyValuePair<TKey, TValue>>
。樹使用Node<T>
,顯然它使用節點之間的引用,所以除了每個鍵和值之外,還將引用左右節點以及一些其他屬性。 Node<T>
是一個類,因此每個實例都有一個引用類型的傳統開銷。此外,每個節點還存儲一個名爲IsRed
的布爾值。
根據WinDbg/SOS,48個字節中的64位單節點的大小,因此它們中的10000個將佔用至少480000個字節。
0:000> !do 0000000002a51d90
Name: System.Collections.Generic.SortedSet`1+Node[[System.Collections.Generic.KeyValuePair`2[[System.Int32, mscorlib],[System.Int32, mscorlib]], mscorlib]]
MethodTable: 000007ff000282c8
EEClass: 000007ff00133b18
Size: 48(0x30) bytes <== Size of a single node
File: C:\windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll
Fields:
MT Field Offset Type VT Attr Value Name
000007feeddfd6e8 4000624 18 System.Boolean 1 instance 0 IsRed
000007feee7fd3b8 4000625 20 ...Int32, mscorlib]] 1 instance 0000000002a51db0 Item
000007ff000282c8 4000626 8 ...lib]], mscorlib]] 0 instance 0000000002a39d90 Left
000007ff000282c8 4000627 10 ...lib]], mscorlib]] 0 instance 0000000002a69d90 Right
歡迎來到.NET運行時。 – 2011-05-11 08:33:57
,因爲它是一棵樹。還有SortedList和一個普通的列表,它們恰好被排序(確保將項目插入到適當的位置) – harold 2011-09-28 09:27:39