C#参考变量纪念品分配 [英] c# reference variable mem allocation

查看:94
本文介绍了C#参考变量纪念品分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道多少内存占用,当你创建一个引用类型变量



一个String =123;



多少内存会的'占用作为参考,而不是指向它?


解决方案
<数据p>这是在下面的方式分解:

 一个String =123; 



变量s:
,这将消耗在当前架构的原生指针大小(被认为是32位,如果操作系统是32位或该过程WOW64下执行),所以32位或64位相应。在这种情况下,s是要么在栈上,或烯注册。 。是你放置字符串引用到一个数组,然后空间将在堆上消耗



事实上,字符串就是一个对象:
8个字节开销分割的4个字节的方法的表,其中兼作什么实际类型的对象是的指示加4个字节用于一些内务的位,并且允许将其用作锁定语句的目标的同步块



该字符串总是由空字符终止(尽管这是一个实现细节不运​​行时的合同的一部分),以便它可以直接与C风格字符串的API使用,人物是UTF-16每性格让两个字节在这个意义上.NET使用字符(为什么是复杂的细节和要求SEGUE成Unicode我应忽略)。



弦进一步包含以下内容:



之前的.NET版本4.0




  • 的int对于字符串的字符长度

  • 为基础数组持有字符

  • 的长度一个int这是第一个字符的字符字符串中(后续字符后,直接)或一个空字符串



字符串可以消耗高达到实际持有字符数组所需要拥有的方式​​StringBuilder的工作。



这样的字符串本身将16 +的消耗需要两倍的内存量(2 * N)+ 2和16 +(4 * N)上取决于它是如何创建堆+ 2个字节。



的.NET 4.0起


  • 的字符串的字符

  • 这是第一个字符的一个字符字符串(后续字符后,直接)或一个空字符串
    空字符


本身将消耗至少12字符串+( 2 * n)的在堆上+ 2个字节。






请注意,在这两种情况下,字符串最多可能需要稍微更实际空间比它使用根据运行时强制执行什么对准,这很可能是不超过IntPtr.Size更



这可以通过串被进一步复杂化实习(其中两个独立的情况下,最终指向相同的字符串,因为它是不可变的),因为你在理论上应该由字符串独立引用数除以堆的开销(加上实习开销)。



有关这个更多的讨论看一看的这篇文章。但是请注意,这篇文章是出于日期为4.0的变化。


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?

解决方案

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.

这篇关于C#参考变量纪念品分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆