0

I asked How do I make a large 3D array without running out of memory?, and I received many answers. All of the answers basically said the same thing, but with one minor difference. Each answer claimed a different size for my 10,000 x 10,000 x 10,000 array. (931 GB, 931 * 4 GB, 4 TB, 3725 GB, etc.)

How do you determine the memory that needs to be allocated for a 3D array?

Community
  • 1
  • 1
Evorlor
  • 7,263
  • 17
  • 70
  • 141
  • 1
    931*4GB is about 4TB, which is about 3725GB. The difference is probably just in terms of 1000 vs 1024 (and 1000*1000 vs 1024*1024 etc). dhke's comment just failed to take into account the element size. – Jon Skeet Mar 24 '15 at 17:37

1 Answers1

1

As java implements multi-dimensional arrays as array of arrays, there are relatively many variables to account for.

The innermost (last dimension) generally follows the formula:

sizeOf = (number of bytes per element) * (number of elements) + (fixed overhead)

Fixed overhead is approximately 12 bytes and the number may need rounding to account for memory granularity, too. For larger arrays both factors contribute very little to array size.

For all other dimensions its the same formula, but the element type is always Reference. Bytes per element works out to either 4 (for 32 bit VM's and 64 bit VM's using compressed OOPS) or 8 (64 bit VM without compressed OOPS).

From there its simple math.

Durandal
  • 19,919
  • 4
  • 36
  • 70