在.Net框架中,2 GiB是可以为对象分配的最高内存。这个事实是独立于平台的(、x64、或x86或.)。我需要一个包含超过2^30个复数的大列表(每个复数是16个字节)。
注意:
和32位Windows操作系统一样,在64位Windows操作系统上运行64位托管应用程序时,可以创建的对象的大小限制为2GB。
来自http://msdn.microsoft.com/en-us/library/ms241064%28VS.80%29.aspx
发布于 2011-10-10 10:35:04
我会在内部实现一个使用多个数组的包装类。
类似于以下内容:
void Main()
{
long count=3*1024*1024;
var arr=new LargeArray<Complex>(count,12);
for(long i=0;i<count;i++)
{
arr[i]=new Complex(i,-i);
}
for(long i=0;i<count;i++)
{
if(arr[i].R!=i)
throw new Exception("Fail"+i+" "+arr[i].R);
}
}
struct Complex
{
public double R,I;
public Complex(double r,double i)
{
R=r;
I=i;
}
}
class LargeArray<T>
{
private readonly int partBits;
private readonly long size;
private T[][] data;
public T this[long index]
{
get
{
if((ulong)index>=(ulong)size)
throw new IndexOutOfRangeException();
int part=(int)(index>>partBits);
int subIndex=(int)(index&((1<<partBits)-1));
return data[part][subIndex];
}
set
{
if((ulong)index>=(ulong)size)
throw new ArgumentOutOfRangeException();
int part=(int)(index>>partBits);
int subIndex=((int)index&((1<<partBits)-1));
data[part][subIndex]=value;
}
}
public LargeArray(long size,int partBits)
{
this.size=size;
this.partBits=partBits;
int partSize=1<<partBits;
int partCount=(int)(((size-1)>>partBits)+1);
data=new T[partCount][];
for(int i=0;i<partCount;i++)
data[i]=new T[partSize];
}
}如果您使用64位进程,则此操作超过2GB。我试了5GB,除了把我的康普换到停顿之外,它还起作用了。
发布于 2011-10-10 10:13:00
然后,您需要以其他方式封装它--创建您自己的集合,它可以处理足够多的数据,并且可能会通过内部委托给较小的集合来使用long而不是int来实现索引值等等。我想这不会很有趣,但我看不出你还有很多其他的选择。
从根本上说,任何由单个数组支持的东西都会遇到问题。
发布于 2011-10-10 10:13:06
改为锯齿状;例如Complex[][]。它可以是宽矩形锯齿,如果你需要,只要总是使用相同的大小为内部数组。
另一种选择;使用大型列表的链接列表;这具有易于扩展的优点。
https://stackoverflow.com/questions/7711258
复制相似问题