Array.Copy --> 不支持大于 2GB 的数组
Array.Copy --> Arrays larger than 2GB are not supported
我正在使用 Array.Copy Method (Array, Array, Int64)。我有以下静态方法
public static T[,] Copy<T>(T[,] a)
where T : new()
{
long n1 = a.GetLongLength(0);
long n2 = a.GetLongLength(1);
T[,] b = new T[n1, n2];
System.Array.Copy(a, b, n1 * n2);
return b;
}
和下面的代码来测试它
double[,] m1 = new double[46340, 46340];
double[,] m2 = Copy(m1); // works
double[,] m3 = new double[46341, 46341];
double[,] m4 = Copy(m3); // Arrays larger than 2GB are not supported
// length argument of Array.Copy
我知道 <gcAllowVeryLargeObjects enabled="true" />
并且我已将其设置为 true。
我在Array.Copy Method (Array, Array, Int64)的文档中看到,对于长度参数有如下注释。
A 64-bit integer that represents the number of elements to copy. The integer must be between zero and Int32.MaxValue, inclusive.
我不明白为什么类型为Int64
时参数长度有这个限制?有解决方法吗?是否计划在即将推出的 .net 版本中取消此限制?
解决方法可能是以下代码
public static T[,] Copy<T>(T[,] a)
where T : new()
{
long n1 = a.GetLongLength(0);
long n2 = a.GetLongLength(1);
long offset = 0;
long length = n1 * n2;
long maxlength = Int32.MaxValue;
T[,] b = new T[n1, n2];
while (length > maxlength)
{
System.Array.Copy(a, offset, b, offset, maxlength);
offset += maxlength;
length -= maxlength;
}
System.Array.Copy(a, offset, b, offset, length);
return b;
}
其中数组被复制成大小为 Int32.MaxValue
.
的块
我正在使用 Array.Copy Method (Array, Array, Int64)。我有以下静态方法
public static T[,] Copy<T>(T[,] a)
where T : new()
{
long n1 = a.GetLongLength(0);
long n2 = a.GetLongLength(1);
T[,] b = new T[n1, n2];
System.Array.Copy(a, b, n1 * n2);
return b;
}
和下面的代码来测试它
double[,] m1 = new double[46340, 46340];
double[,] m2 = Copy(m1); // works
double[,] m3 = new double[46341, 46341];
double[,] m4 = Copy(m3); // Arrays larger than 2GB are not supported
// length argument of Array.Copy
我知道 <gcAllowVeryLargeObjects enabled="true" />
并且我已将其设置为 true。
我在Array.Copy Method (Array, Array, Int64)的文档中看到,对于长度参数有如下注释。
A 64-bit integer that represents the number of elements to copy. The integer must be between zero and Int32.MaxValue, inclusive.
我不明白为什么类型为Int64
时参数长度有这个限制?有解决方法吗?是否计划在即将推出的 .net 版本中取消此限制?
解决方法可能是以下代码
public static T[,] Copy<T>(T[,] a)
where T : new()
{
long n1 = a.GetLongLength(0);
long n2 = a.GetLongLength(1);
long offset = 0;
long length = n1 * n2;
long maxlength = Int32.MaxValue;
T[,] b = new T[n1, n2];
while (length > maxlength)
{
System.Array.Copy(a, offset, b, offset, maxlength);
offset += maxlength;
length -= maxlength;
}
System.Array.Copy(a, offset, b, offset, length);
return b;
}
其中数组被复制成大小为 Int32.MaxValue
.