Why does System.Int32 take 24 bytes?
Asked Answered
B

1

6

If an int takes 4 bytes, why does System.Int32 takes 24 bytes when boxing an integer into an object?

For example:

int i = 3;
object o = i;
Bobbybobbye answered 10/1, 2022 at 20:46 Comment(4)
exactly how do you get those numbers?Shanon
I'm curious how you are measuring your 24 bytes (you don't show that). The name o is an object variable (i.e., a reference to a reference type instance), so the object it refers to (the boxed int) gets allocated on the managed heap and o ends up being a reference type variable that holds a reference to that objectHofstetter
"boxing" means putting the value type into an object box. It's not just a cast: it effectively adds a pointer and other trappings involved in having a full object reference. In other words, the box itself has some overhead.Pseudoscope
Are you drawing a distinction between int and System.Int32 with that wording?Khalkha
A
16

In C#/CLR reference types have some size overhead:

The layout of a managed object is pretty simple: a managed object contains instance data, a pointer to a meta-data (a.k.a. method table pointer) and a bag of internal information also known as an object header.

enter image description here

So when you box your int into the heap it will be stored as a reference type and will include all the extra info and then the memory will be aligned resulting in something like this:

Object Header (8 bytes)
Method Table Pointer (8 bytes)
Int (4 bytes)
padding (4 bytes, to align at 8 bytes)

Note that this is dependent on the bitness of the process (the breakdown above is for 64-bit case) and is actually runtime implementation detail (the previously linked article mentions at least one implementation that differs).

Acanthus answered 10/1, 2022 at 20:53 Comment(4)
Correct, but it is really an implementation detail of the runtime. You know the boxed value will have a header etc., but exactly how big that header is, is an implementation detail. Also, not include above, is the size of the reference itself, i.e. the "number" (suposedly) which act as some sort of address to tell where the box is located. Again an implementation detail, but the reference could be a 32-bit or a 64-bit number.Illampu
My private implementation of .NET uses pen and paper, and each byte is written in cuneiform. It is a perfect implementation of ECMA-335, but does not use 24 bytes for an int32. Now what? We shouldn't really care where or how our data is stored in memory, or even if it exists. It could be on the moon for it should make a differenceAussie
"Now what? We shouldn't really care where or how our data is stored in memory, or even if it exists. It could be on the moon for it should make a difference." @charlieface Why should we learn to do arithmetic when we can just use a calculator? What a terrible message to send a curious person trying to better understand how a computer works under the hood.Durango
@PeterMoore It wasn't to say that we shouldn't learn, we should learn how implementations and abstractions work, but that we should also be aware that it's completely implementation-defined. This is a good answer, but the point in the the first comment above by needs to be stressed more. Otherwise beginners will think: this is how it always works, when that is absolutely not the caseAussie

© 2022 - 2024 — McMap. All rights reserved.