元组与int [2]内存使用情况

时间:2021-10-03 16:00:17

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 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

32位上的大小(由分析确定):元组 :16字节int [2]:20字节int [1到2] *:28字节int [2,1]:36字节在64- bit CLR:Tuple :24字节int [2]:32字节int [1到2] *:40字节int [2,1]:48字节 ,int> ,int>

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:

* 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 使用与具有两个整数字段的类相同的内存。另一方面,数组具有相当大的内部数据结构(在SSCLI中称为ArrayOpScript),其至少为32字节加上另一个数据结构(称为ArrayOpIndexSpec),用于每个大小为16字节的等级(在本例中为1)。因此,阵列几乎肯定会使用多个因素而不是元组。 ,int>

#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 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

32位上的大小(由分析确定):元组 :16字节int [2]:20字节int [1到2] *:28字节int [2,1]:36字节在64- bit CLR:Tuple :24字节int [2]:32字节int [1到2] *:40字节int [2,1]:48字节 ,int> ,int>

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:

* 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 使用与具有两个整数字段的类相同的内存。另一方面,数组具有相当大的内部数据结构(在SSCLI中称为ArrayOpScript),其至少为32字节加上另一个数据结构(称为ArrayOpIndexSpec),用于每个大小为16字节的等级(在本例中为1)。因此,阵列几乎肯定会使用多个因素而不是元组。 ,int>