元组LT; INT,INT>与INT [2]内存使用情况 [英] Tuple<int, int> versus int[2] memory usage

查看:122
本文介绍了元组LT; INT,INT>与INT [2]内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道这两者之间的内存区别?或者,如何能弄明白容易自己?

Does anybody know the memory difference between these two? Or how one would figure it out easily themselves?

推荐答案

有关的32位的CLR,两者将具有4个字节的锁,4个字节的类型手柄,和8个字节为两个整数。阵列,但是,将有一个额外的4个字节来存储的长度(2在这种情况下),所以该阵列将有4个字节的开销。

For a 32-bit CLR, both will have 4 bytes for the lock, 4 bytes for the type handle, and 8 bytes for the two ints. The array, however, will have an extra 4 bytes to store the length (2 in this case), so the array will have 4 bytes overhead.

尺寸在32位(通过分析确定):
元组LT; INT,INT> :16字节
INT [2] :20字节
INT [1〜2] *:28字节
INT [2,1] :36字节
在64位的CLR:
元组LT; INT,INT> :24字节
INT [2] :32字节
INT [1〜2] *:40字节
INT [2,1] :48字节

Sizes (determined by profiling) on 32-bit:
Tuple<int, int>: 16 bytes
int[2]: 20 bytes
int[1 to 2]*: 28 bytes
int[2, 1]: 36 bytes
On a 64-bit CLR:
Tuple<int, int>: 24 bytes
int[2]: 32 bytes
int[1 to 2]*: 40 bytes
int[2, 1]: 48 bytes

需要注意的是值类型的单维从零开始的数组中最小的阵列。使用引用类型增加了另一个4字节为对象的类型被存储(8个字节在64位)。使用非零阵基地或多个维度使得它使用另一种数组类型的存储等级和下限的信息,每个维度增加8个额外的字节。

Note that single-dimensional zero-based arrays of value types are the smallest possible arrays. Using reference types adds another 4 bytes for the type of object being stored (8 bytes on 64-bit). Using non-zero array bases or multiple dimensions makes it use another kind of array type that stores the rank and lower-bound information, adding 8 additional bytes per dimension.

参考文献:

  • http://www.codeproject.com/KB/dotnet/arrays.aspx
  • http://www.codeproject.com/KB/cs/net_type_internals.aspx?fid=459323&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2567811
  • http://msdn.microsoft.com/en-us/magazine/cc301755.aspx

*您不能声明与非0的下限在C#中的数组,所以我做了语法 INT [1〜2] 。不过你可以叫 Array.CreateInstance(typeof运算(INT),新的[] {2},新的[] {10}); 以创建一个包含有2个元素,在因为这样的阵列不能再直接在C#的类型系统presented指数10和11。当然,他们并不十分有用,但它们提供了一个有趣的数据点。

* You can't declare an array with a non-0 lower bound in C#, so I made up the syntax int[1 to 2]. You can, however call Array.CreateInstance(typeof(int), new[]{2}, new[]{10}); to create an array with 2 elements, at index 10 and 11. Of course since such arrays cannot be represented directly in C#'s type system, they aren't terribly useful, but they provide an interesting data point.

这篇关于元组LT; INT,INT&GT;与INT [2]内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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