Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is broken down in the following fashion:</p> <pre><code>String s = "123"; </code></pre> <p>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.</p> <p>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.</p> <p>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).</p> <p>Strings further contain the following:</p> <h3>Versions of .Net prior to 4.0</h3> <ul> <li>an int for the length of the string in characters</li> <li>an int for the length of the underlying array holding the characters</li> <li>a character which is the first character in the string (subsequent characters are directly after it) or the null character for an empty string</li> </ul> <p>The string <strong>may</strong> consume up to twice the amount of memory required to actually hold the character array needed owning to the way StringBuilder's work</p> <p>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.</p> <h3>Versions of .Net from 4.0 onwards</h3> <ul> <li>an int for the length of the string in characters</li> <li>a character which is the first character in the string (subsequent characters are directly after it) or the null character for an empty string</li> </ul> <p>The string itself will consume at least 12 + (2*n) + 2 bytes on the heap.</p> <hr> <p>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.</p> <p>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. </p> <p>for more discussion of this take a look at <a href="http://www.codeproject.com/KB/dotnet/strings.aspx" rel="nofollow noreferrer">this article</a>. Note however that this article is out of date for the changes in 4.0.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload