一种用于大型多维数组(交错数组)C#解决方法吗? [英] A workaround for a big multidimensional array (Jagged Array) C#?
问题描述
我想在三维初始化数组加载一个体素的世界。
在地图的总大小应为( 2048/1024/2048 )。我试图初始化INT的交错数组但是我抛出一个内存异常。什么是大小限制?
我的工作台尺寸:2048 * 1024 * 2048 = 4'191'893'824
任何人都知道有没有解决这一问题的方法?
//的System.OutOfMemoryException在这里!
INT [] [] []矩阵的计算= CreateJaggedArray< INT [] [] []>(2048,1024,2048);
//如果我尝试正常初始化我也抛出异常
INT [,]矩阵的计算=新INT [2048,1024,2048] 静态ŧCreateJaggedArray< T>(PARAMS INT []长度)
{
返回(T)InitializeJaggedArray(typeof运算(T).GetElementType(),0,长度);
} 静态对象InitializeJaggedArray(类型类型,INT指数,INT []长度)
{
阵列阵列= Array.CreateInstance(类型,长度[指数]);
键入的ElementType = type.GetElementType(); 如果(元素类型!= NULL)
{
的for(int i = 0; I<长度[指数];我++)
{
array.SetValue(
InitializeJaggedArray(元素类型,指数+ 1,长度),I);
}
} 返回数组;
}
谢谢你这么多以谁试图帮助我了解和解决我的问题。在全体员工
我尝试了几种解决方案,以便能够装载大量的数据,并存储在表中。
两天后,这里是我的测试,最后的解决方案其中可以4'191'893'824 项的存储到一个阵列
添加我的最终解决方案,希望有人能帮
目标
我记得的目标:初始化一个整数数组[2048/1024/2048]存储的 4'191'893'824 数据
试验1:与JaggedArray方法(失败)
系统出抛出内存异常
/ * * ********** /
/ *铁血阵列方法* /
/ * * ********** / //分配第一个维度;
bigData =新INT [2048] [] [];
为(中间体X = 0; X&下; 2048; X ++)
{
//分配第二个维度;
bigData [X] =新INT [1024] [];
对于(INT Y = 0; Y< 1024; Y ++)
{
//最后一维分配
bigData [X] [Y] =新的INT [2048];
}
}
测试2:用列表法(失败)
系统出抛出内存异常(分大阵分成几个小阵..不因为工作列出<>最多允许2GB拉姆训示像一个简单的数组不幸的。)
/ * * ********** /
/ *列表的方法* /
/ * * ********** / 清单< INT [,,]> bigData =新的List< INT [,,]>(512);
对于(int类型的= 0; A< 512; A ++)
{
bigData.Add(新INT [256,128,256]);
}
测试3:MemoryMappedFile(解决方案)
我终于终于找到了解决方案!
使用类内存映射文件包含了虚拟内存文件的内容。
MemoryMappedFile MSDN
使用自定义类使用上我$ C $的CProject的此处。初始化很长,但它工作得很好!
/ * * ************************ /
/ * MemoryMappedFile方法* /
/ * * ************************ / 字符串路径= AppDomain.CurrentDomain.BaseDirectory;
VAR myList中=新GenericMemoryMappedArray< INT>(2048L * 1024L * 2048L,路径);
使用(myList中)
{
myList.AutoGrow = FALSE; / *
为(int类型的= 0;一个≤(2048L * 1024L * 2048L);一++)
{
myList中[A] = A;
}
* / myList中[12456] = 8;
myList中[1939848234] = 1;
//等等...
}
I'm trying to initialize an array in three dimension to load a voxel world.
The total size of the map should be (2048/1024/2048). I tried to initialize an jagged array of "int" but I throw a memory exception. What is the size limit? Size of my table: 2048 * 1024 * 2048 = 4'191'893'824
Anyone know there a way around this problem?
// System.OutOfMemoryException here !
int[][][] matrice = CreateJaggedArray<int[][][]>(2048,1024,2048);
// if i try normal Initialization I also throws the exception
int[, ,] matrice = new int[2048,1024,2048];
static T CreateJaggedArray<T>(params int[] lengths)
{
return (T)InitializeJaggedArray(typeof(T).GetElementType(), 0, lengths);
}
static object InitializeJaggedArray(Type type, int index, int[] lengths)
{
Array array = Array.CreateInstance(type, lengths[index]);
Type elementType = type.GetElementType();
if (elementType != null)
{
for (int i = 0; i < lengths[index]; i++)
{
array.SetValue(
InitializeJaggedArray(elementType, index + 1, lengths), i);
}
}
return array;
}
Thank you so much to all the staff who tried to help me in understanding and solving my problem.
I tried several solution to be able to load a lot of data and stored in a table. After two days, here are my tests and finally the solution which can store 4'191'893'824 entry into one array
I add my final solution, hoping someone could help
the goal
I recall the goal: Initialize an integer array [2048/1024/2048] for storing 4'191'893'824 data
Test 1: with JaggedArray method (failure)
system out of memory exception thrown
/* ******************** */
/* Jagged Array method */
/* ******************** */
// allocate the first dimension;
bigData = new int[2048][][];
for (int x = 0; x < 2048; x++)
{
// allocate the second dimension;
bigData[x] = new int[1024][];
for (int y = 0; y < 1024; y++)
{
// the last dimension allocation
bigData[x][y] = new int[2048];
}
}
Test 2: with List method (failure)
system out of memory exception thrown (divide the big array into several small array .. Does not work because "List <>" allows a maximum of "2GB" Ram allocution like a simple array unfortunately.)
/* ******************** */
/* List method */
/* ******************** */
List<int[,,]> bigData = new List<int[,,]>(512);
for (int a = 0; a < 512; a++)
{
bigData.Add(new int[256, 128, 256]);
}
Test 3: with MemoryMappedFile (Solution)
I finally finally found the solution! Use the class "Memory Mapped File" contains the contents of a file in virtual memory.
MemoryMappedFile MSDN Use with custom class that I found on codeproject here. The initialization is long but it works well!
/* ************************ */
/* MemoryMappedFile method */
/* ************************ */
string path = AppDomain.CurrentDomain.BaseDirectory;
var myList = new GenericMemoryMappedArray<int>(2048L*1024L*2048L, path);
using (myList)
{
myList.AutoGrow = false;
/*
for (int a = 0; a < (2048L * 1024L * 2048L); a++)
{
myList[a] = a;
}
*/
myList[12456] = 8;
myList[1939848234] = 1;
// etc...
}
这篇关于一种用于大型多维数组(交错数组)C#解决方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!