如果我从未实例化某些子数组,为2D数组分配了多少空间?

时间:2022-02-11 21:38:23

So I'm working on a Java program that is going to be updating a 2D array many times per second. Originally, I was copying the entirety of the array using System.arrayCopy(), but that was clearly a bad idea since I'm only updating one row at a time. Instead I decided to just have a "pointer" to the virtual "zero" of the array. Anyway, I also want to be able to support dynamic sizing, and it occurred to me that the easiest way to do this might be to have one array that stays the same size, and then have a number that indicates the maximum length of the virtual array. I found myself thinking that could be a waste of memory though

所以我正在开发一个Java程序,它将每秒多次更新2D数组。最初,我使用System.arrayCopy()复制整个数组,但这显然是一个坏主意,因为我一次只更新一行。相反,我决定只有一个“指针”指向数组的虚拟“零”。无论如何,我还希望能够支持动态大小调整,并且我想到最简单的方法可能是让一个数组保持相同的大小,然后有一个数字表示虚拟的最大长度阵列。我发现自己认为这可能是浪费记忆

The above information is just so someone can tell me if I'm doing something completely ridiculous.

以上信息只是有人可以告诉我,我做的事情是否完全荒谬。

TL;DR = if I do the following...

TL; DR =如果我做以下......

int[][] data = new int[2000][]; // 2000 would be the max
for(int i=0; i<10; i++) {
    data[i] = new int[10];
}

...how much memory have I used in addition to the hundred ints I actually initialized? Would it just be the 4 bytes for each null reference?

...除了我实际初始化的一百个整数之外,我还使用了多少内存?它只是每个空引用的4个字节吗?

3 个解决方案

#1


2  

Its 4 bytes for every null reference plus 8-16 bytes for the header. Most 64-bit JVMs use 32-bit references up to about 30 GB of heap size.

每个空引用的4个字节加上标头的8-16个字节。大多数64位JVM使用32位引用,最大堆大小约为30 GB。

#2


1  

Consider using a java.util.List that contains other Lists as elements instead of a 2D array (which is an array that contains other arrays in Java).

考虑使用包含其他列表的java.util.List作为元素而不是2D数组(这是一个包含Java中其他数组的数组)。

The Lists

  • will be more intelligent out of the box
  • 开箱即用将更加智能化

  • will automatically support dynamic sizing (in a performance-optimized manner)
  • 将自动支持动态大小调整(以性能优化的方式)

  • can contain nulls (like the uninitialized subarrays)
  • 可以包含空值(如未初始化的子数组)

  • you can tweak their performance according to your requirements (ArrayList vs LinkedList vs CopyOnWriteArrayList)...
  • 你可以根据你的要求调整他们的表现(ArrayList vs LinkedList vs CopyOnWriteArrayList)...

#3


-1  

Yes, you will use 2000*sizeof(ptr) bytes for the first line, and 10*10*sizeof(int) for the loop (plus object overhead).

是的,您将为第一行使用2000 * sizeof(ptr)字节,为循环使用10 * 10 * sizeof(int)(加上对象开销)。

#1


2  

Its 4 bytes for every null reference plus 8-16 bytes for the header. Most 64-bit JVMs use 32-bit references up to about 30 GB of heap size.

每个空引用的4个字节加上标头的8-16个字节。大多数64位JVM使用32位引用,最大堆大小约为30 GB。

#2


1  

Consider using a java.util.List that contains other Lists as elements instead of a 2D array (which is an array that contains other arrays in Java).

考虑使用包含其他列表的java.util.List作为元素而不是2D数组(这是一个包含Java中其他数组的数组)。

The Lists

  • will be more intelligent out of the box
  • 开箱即用将更加智能化

  • will automatically support dynamic sizing (in a performance-optimized manner)
  • 将自动支持动态大小调整(以性能优化的方式)

  • can contain nulls (like the uninitialized subarrays)
  • 可以包含空值(如未初始化的子数组)

  • you can tweak their performance according to your requirements (ArrayList vs LinkedList vs CopyOnWriteArrayList)...
  • 你可以根据你的要求调整他们的表现(ArrayList vs LinkedList vs CopyOnWriteArrayList)...

#3


-1  

Yes, you will use 2000*sizeof(ptr) bytes for the first line, and 10*10*sizeof(int) for the loop (plus object overhead).

是的,您将为第一行使用2000 * sizeof(ptr)字节,为循环使用10 * 10 * sizeof(int)(加上对象开销)。