多维数组的限制
我刚刚创建了一个 1024 x 1024 x 1024 的多维数组。我收到 OutOfMemory
异常。可以安全使用的最大尺寸多维数组是多少?我在 VB.net 中执行此操作,因此所有 .net 答案都是可以接受的。
编辑
当我说安全时,我的意思是,尺寸适合任何计算机。多大的尺寸才能在 32 位操作系统上顺利运行。没想到1024大小是4G。我希望能得到其中的十六分之一。
I just made a multidimensional array that was 1024 x 1024 x 1024. I get an OutOfMemory
exception. What is the largest size multidimensional array that is safe to use? I'm doing this in VB.net so all .net answers are acceptable.
EDIT
When I said safe, I mean, a good size for just about any computer. What size would run smoothly on a 32bit operating system. I didn't realize that the 1024 size was 4G. I'm hoping for something a 16th of that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
.NET 对象不能大于 2 GB。即使如此,在 32 位操作系统上,也不太可能在可寻址虚拟内存空间中找到足够大的空间来容纳如此大的数组。程序启动后,您可以获得大约 600 MB 的内存,在地址空间开始碎片化后,内存会迅速下降。当还有半个可用空间时,对 90 MB 分配进行轰炸并不罕见。
您需要使用锯齿状数组 和 64 位操作系统。如果您确实需要如此庞大的数组,请进行一些反省。 System.Collections.Generic 可用后,数组就有点老派了。使用与数组一样快的集合类并且仅让您为实际使用的内容付费。受到推崇的。
.NET objects can't be larger than 2 gigabytes. Even then, on a 32-bit operating system it is very unlikely to find a hole in addressable virtual memory space large enough to fit such a large array. You can get about 600 megabytes right after your program starts, rapidly going down hill from there after the address space starts getting fragmented. Bombing on a 90 MB allocation when there's still half a gig free space is not terribly unusual.
You'll need to use jagged arrays and a 64-bit operating system. And a bit of introspection if you really need such a massive array. Arrays are kinda old school after System.Collections.Generic became available. With collection classes that are as fast as an array and only let you pay for what you actually use. Recommended.
嗯,1024 * 1024 * 1024 是一个非常大的数字。假设您使用的是整数数组,则这相当于 4 GiB 内存,不包括管理多个数组的开销。
由于没有进程可以分配超过 2 GiB 的内存,因此您已经超出了操作系统(不是 VB 或 .NET!)施加的硬限制。 .NET 本身没有真正的限制,因为达到机器限制的速度要快得多。
Well, 1024 * 1024 * 1024 is a very large number. Assume you are using an integer array, then this corresponds to 4 GiB of memory, not counting the overhead of managing the multiple arrays.
Since no process may allocate more than 2 GiB of memory, you’ve surpassed the hard limit imposed by the operating system (not VB or .NET!). .NET itself has no real limit here since the machine limit is reached much faster.
如果没有至少两个其他详细信息,这个问题就没有意义:
获取这些数字,然后进行数学计算。假设未装箱的 32 位整数,一个 1024 * 1024 * 1024 数组将消耗大约 4 GB (实际上更多,.NET 数组不是 C 数组,并且有一些开销;我对它们的了解不够)实现来估计开销有多大)。您可以使用这样的数组,如果您希望将程序的使用限制在具有大量(超过4 GB,至少是因为您的程序赢得了)的64位计算机上不是唯一一个正在运行的)内存量。您希望运行程序的计算机可能没有那么强大。然后,您必须计算出您想要/需要支持的最小值,并进行一些数学计算来估计您可以舒适地消耗多少内存。
This question is meaningless without at least two other details:
Get those number, then do the math. Assuming unboxed 32 bit integers, a 1024 * 1024 * 1024 array would consume roughly 4 GB (actually more, .NET arrays aren't C arrays and have some overhead; I don't know enough about their implementation to estimate how large the overhead is). You can use such an array, if you want to restrict use of the program to 64 bit computers with vast (more than 4 GB, at the very least because you program won't be the only one running) amounts of memory. Likely the computers you intend your program to run on aren't that powerful. Then you'll have to figure out the minimum you want/need to support and do some math to estimate how much memory you can comfortably consume.