Vince
Vince

Reputation:

c# reference variable mem allocation

Does anyone know how much memory is taken up when you create a reference type variable?

String s = "123";

How much memory would 's' take up as a reference, not the data pointing to it?

Upvotes: 6

Views: 4966

Answers (5)

ShuggyCoUk
ShuggyCoUk

Reputation: 36438

This is broken down in the following fashion:

String s = "123";

The variable s: this will consume the native pointer size on the current architecture (which is considered 32bit if the OS is 32bit or the process is executing under WoW64), so 32 bits or 64 bits accordingly. In this case s is either on the stack, or en-registered. Were you to place the string reference into an array then that space would be consumed on the heap.

The fact that string is an object: 8 bytes of overhead split 4 bytes for the method table, which doubles as the indication of what actual type an object is plus 4 bytes for some housekeeping bits and the syncblock that allows it to be used as the target of a lock statement.

The string is always terminated by the null character (though this is an implementation detail not part of the contract of the runtime) so that it can be used directly with C-Style string apis, characters are UTF-16 so two bytes per character in the sense .Net uses character (the details of why is complicated and requires a segue into Unicode I shall omit).

Strings further contain the following:

Versions of .Net prior to 4.0

  • an int for the length of the string in characters
  • an int for the length of the underlying array holding the characters
  • a character which is the first character in the string (subsequent characters are directly after it) or the null character for an empty string

The string may consume up to twice the amount of memory required to actually hold the character array needed owning to the way StringBuilder's work

Thus the string itself will consume between 16 + (2*n) + 2 and 16 + (4*n) + 2 bytes on the heap depending on how it was created.

Versions of .Net from 4.0 onwards

  • an int for the length of the string in characters
  • a character which is the first character in the string (subsequent characters are directly after it) or the null character for an empty string

The string itself will consume at least 12 + (2*n) + 2 bytes on the heap.


Note that in both cases the string may take up slightly more actual space than it uses depending on what alignment the runtime enforces, this is likely to be no more than the IntPtr.Size.

This may be further complicated by string interning (where two separate instances end up pointing to the same string since it is immutable) since you should in theory divide the heap overhead (plus the intern overhead) by the number of 'independent' references to the string.

for more discussion of this take a look at this article. Note however that this article is out of date for the changes in 4.0.

Upvotes: 11

David Morton
David Morton

Reputation: 16505

If you want to check this in code, call:

IntPtr.Size

Upvotes: 1

Stu Mackellar
Stu Mackellar

Reputation: 11638

The size of the reference itself will depend on your processor architecture - 4 bytes on 32-bit, 8 bytes on 64-bit.

Upvotes: 10

AnthonyWJones
AnthonyWJones

Reputation: 189457

Typicall 4 bytes for the reference is needed

Upvotes: 1

mqp
mqp

Reputation: 71945

Depending on whether you're on a 32- or 64-bit machine, it'll be either a 32- or 64-bit pointer.

Upvotes: 4

Related Questions