When the CLR places an object on the Large Object Heap, is it an "all or nothing" deal? Are class/struct members "split up" and placed in different heaps?
class OneBigObject
{
byte[] bigObject;
public OneBigObject()
{
bigObject = new byte[100000];
}
}
class TwoSmallObjects
{
byte[] smallObject1;
byte[] smallObject2;
public TwoSmallObjects()
{
smallObject1 = new byte[50000];
smallObject2 = new byte[50000];
}
}
class MixedSizeObjects
{
byte[] smallObject1;
byte[] smallObject2;
byte[] bigObject;
public MixedSizeObjects()
{
smallObject1 = new byte[50000];
smallObject2 = new byte[50000];
bigObject = new byte[100000];
}
}
OneBigObject oneBigObject = new OneBigObject();
TwoSmallObjects twoObjects = new TwoSmallObjects();
MixedSizeObjects mixedSizeObjects = new MixedSizeObjects();
Is TwoSmallObjects
placed on the Large Object Heap since its total size is over 85,000 bytes? Even though both members are individually under the threshold? What about MixedSizeObjects
?
Each of the byte arrays that you are allocating are treated separately from the enclosing class. So the OneBigObject is actually two different CLR objects. One is the OneBigObject instance that is very small and contains just a reference field. The other is the actual byte array of 100,000 instances. The same principle applies to the other classes as well.
Classes and structs are not split up. There is no need because it is hard to imagine anyone creating a type that has enough actual fields to make it 85k in storage size. Large looking objects, like your example, actually consist of references and arrays of references and so are not very big at all.