NeddySpaghetti
NeddySpaghetti

Reputation: 13495

Why is the minimum size of a reference type 12 bytes for a 32 bit .NET process

I was reading the Pro .Net Performance book section on reference type internals. It mentions that for a 32 bit .net process a reference type has 4 bytes of object header and 4 bytes of method table pointer. Also, says that on a 32 bit system, the objects are aligned to the nearest 4 byte multiple, which makes the minimum size of a reference type 12 bytes.

My question is, why is the minimum size 12 bytes? The object is 8 bytes and that already aligns with a 4 byte boundary.

Upvotes: 6

Views: 786

Answers (2)

weston
weston

Reputation: 54801

Minimum of 12 bytes is a requirement of the garbage collection implementation.

From here: http://msdn.microsoft.com/en-us/magazine/cc163791.aspx#S9

The Base Instance Size is the size of the object as computed by the class loader, based on the field declarations in the code. As discussed previously, the current GC implementation needs an object instance of at least 12 bytes. If a class does not have any instance fields defined, it will carry an overhead of 4 bytes. The rest of the 8 bytes will be taken up by the Object Header (which may contain a syncblk number) and TypeHandle.

(TypeHandle being a handle to the method table).

Upvotes: 6

Jim Mischel
Jim Mischel

Reputation: 134035

So you have 8 bytes of overhead (the object header and the method table pointer). If you want any data in the object, then you need at least one more byte, and because memory is allocated to objects in 4-byte chunks, you end up with a minimum of 12 bytes.

Upvotes: 0

Related Questions