开发者

Limits for Multidimensional Arrays

开发者 https://www.devze.com 2023-04-07 10:57 出处:网络
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开发者_StackOverflow社区 to use?I\'m doing t

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开发者_StackOverflow社区 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.


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


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.


This question is meaningless without at least two other details:

  • Available memory on the computer
  • Size of each memory

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号