Does anybody know the memory difference between these two? Or how one would figure it out easily themselves?
有人知道这两者之间的记忆差异吗?或者人们如何轻易地解决这个问题?
2 个解决方案
#1
14
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位CLR,两者都有4个字节用于锁定,4个字节用于类型句柄,8个字节用于两个整数。但是,该数组将有额外的4个字节来存储长度(在本例中为2),因此该数组将具有4个字节的开销。
Sizes (determined by profiling) on 32-bit:Tuple<int, int>
: 16 bytesint[2]
: 20 bytesint[1 to 2]
*: 28 bytesint[2, 1]
: 36 bytes
On a 64-bit CLR:Tuple<int, int>
: 24 bytesint[2]
: 32 bytesint[1 to 2]
*: 40 bytesint[2, 1]
: 48 bytes
32位上的大小(由分析确定):元组
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.
请注意,值类型的一维零基数组是可能的最小数组。使用引用类型为存储的对象类型添加另外4个字节(64位上为8个字节)。使用非零数组基数或多维使其使用另一种存储排名和下限信息的数组类型,每个维度增加8个额外字节。
References:
- 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
* 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.
*你不能在C#中声明一个非0下限的数组,所以我编写了语法int [1到2]。但是,您可以调用Array.CreateInstance(typeof(int),new [] {2},new [] {10});在索引10和11处创建一个包含2个元素的数组。当然,由于这些数组不能直接在C#的类型系统中表示,它们并不是非常有用,但它们提供了一个有趣的数据点。
#2
0
A Tuple<int, int>
uses the same memory as class with two integer fields. An array on the other hand, has a fairly large internal data structure (called ArrayOpScript
in the SSCLI) that is at least 32-bytes plus another data structure (called ArrayOpIndexSpec
) for each rank (in this case one) of size 16 bytes. So an array almost certainly uses several factors more memory than a Tuple
.
Tuple
#1
14
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位CLR,两者都有4个字节用于锁定,4个字节用于类型句柄,8个字节用于两个整数。但是,该数组将有额外的4个字节来存储长度(在本例中为2),因此该数组将具有4个字节的开销。
Sizes (determined by profiling) on 32-bit:Tuple<int, int>
: 16 bytesint[2]
: 20 bytesint[1 to 2]
*: 28 bytesint[2, 1]
: 36 bytes
On a 64-bit CLR:Tuple<int, int>
: 24 bytesint[2]
: 32 bytesint[1 to 2]
*: 40 bytesint[2, 1]
: 48 bytes
32位上的大小(由分析确定):元组
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.
请注意,值类型的一维零基数组是可能的最小数组。使用引用类型为存储的对象类型添加另外4个字节(64位上为8个字节)。使用非零数组基数或多维使其使用另一种存储排名和下限信息的数组类型,每个维度增加8个额外字节。
References:
- 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
* 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.
*你不能在C#中声明一个非0下限的数组,所以我编写了语法int [1到2]。但是,您可以调用Array.CreateInstance(typeof(int),new [] {2},new [] {10});在索引10和11处创建一个包含2个元素的数组。当然,由于这些数组不能直接在C#的类型系统中表示,它们并不是非常有用,但它们提供了一个有趣的数据点。
#2
0
A Tuple<int, int>
uses the same memory as class with two integer fields. An array on the other hand, has a fairly large internal data structure (called ArrayOpScript
in the SSCLI) that is at least 32-bytes plus another data structure (called ArrayOpIndexSpec
) for each rank (in this case one) of size 16 bytes. So an array almost certainly uses several factors more memory than a Tuple
.
Tuple