.NET Collections and the Large Object Heap (LOH)

孤街醉人 提交于 2019-11-28 12:06:40

Objects will only be stored on the LOH if they are over 85,000 bytes. A large list (especially of structs) will often get allocated here.

However, Dictionary's are less likely, since they're storing an array of buckets, so unless the generate enough buckets so the array becomes >85000 bytes, it's unlikely. A list of 40k elements will be stored on the LOH, even if they're classes (since the object references in each element will cause the list to be 160k on x86, 320k on x64 systems). The individual elements will be on the standard heap, though, so will get compacted, etc.

If you are using a doubly linked list instead of a standard List, it is very unlikely that it will get stored on the LOH. Each element of the list will be small (just a single node with references to the next/previous nodes), so no single object will be >85k bytes.

For details on the LOH, this is a great blog entry.

System.Collections.Generic.List is implemented as an array internally, not a linked list. And yes, if the size of the collection is large it'll be allocated on large object heap (note that the size of the array is important, if you have a small array of large reference types, it won't be allocated on LOH).

List is implemented as an array. As such the array will be put into LOH, but the List object itself will not.

The same basically applies to Dictionary as well. It too is using an array of buckets internally, which basically store the key/value pairs you add.

Dictionary has O(LOG N) vector for the key/value, so in 40K+ objects your pretty safe. As said here before List is implemented as array so big list are indeed on the LOH. You can check if you object is on the LOH using SOS.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!