Reputation: 871
I have a question about about when objects get put into the small- vs large-object-heap.
I have two string with size smaller than 85kB which I add to a collection, such as a List. The combined size of the strings exceeds 85kB.
Will this collection be treated as part of the small-object-heap or as part of the large object-heap? I think it will be SOH but I'm not sure.
Upvotes: 0
Views: 228
Reputation: 111940
If the collection is a List<TReference>
of any size then it is SOH. If the collection is a TReference[]
with a size <= 20k elements at 32 bits, or 10k elements at 64 bits (the size of a reference at 32bits is 4 bytes, at 64bits is 8 bytes), the it is SOH.
The size of an object isn't calculate based on object X + all the objects referenced by object X, but simply by the "direct" size of X.
Why the difference between List<TReference>
and TReference[]
? Because arrays (technically single dimensional, 0-base indexed arrays) are the only variable length data type directly supported by the IL language. Nearly all the other collection types are simply a reference to a T[]
array (or to a bunch of T[]
arrays) plus some supporting numeric fields. So for example a List<T>
is a reference to a T[]
plus a int Length
.
The point of SOH vs LOH is "how much fast would be to move it around memory"? Clearly:
public class MySmallClass
{
public byte[] WithABigObject = new byte[1024*1024];
}
While moving the BigObject
(the byte array) would be very slow, moving the MySmallClass
(that is simply composed by a reference) is very fast, because it has the size of a reference :-)
Note that in C# creating objects that aren't arrays with a size >= 85k is quite difficult, because, as I've said, between the types that are directly supported by the IL language, only the array has a variable size. Every other type has a fixed size and is quite small. You would need to make a class with 22k int
inside to make a LOH without an array :-)
An example of a LOH object without arrays: https://ideone.com/OuVkEs . It is based on multilevel KeyValuePair<,>
(a struct
) that at the lowest level use Guid
(the biggest commonly used struct
). The resulting BigGuid128k
is a struct
of 128k. By including it in your class, your class will become a LOH :-)
Upvotes: 1