1

我正在使用 java-Xmx60g命令运行以下代码。

每个阵列应该是8.5GB左右,总共17GB。该机器总共有64GB,其中63GB “免费”。它打印DONE DECLARING RIVER HANDS 1,表明它完成了第一个数组的声明。

但是我在线程“main” java.lang.OutOfMemoryError: Java heap space 在第二个数组的声明过程中得到了异常。我很困惑,因为我的理解是-Xmx60g应该分配60GB,而数组只使用17GB

在此先感谢您的帮助!

long NUM_RIVER_HANDS = 2428287420L;
int half_river = (int)(NUM_RIVER_HANDS/2);
byte[][] river_hands_1 = new byte[half_river][7];
System.out.println("DONE DECLARING RIVER HANDS 1");
byte[][] river_hands_2 = new byte[(int)(NUM_RIVER_HANDS - half_river)][7];
System.out.println("DONE DECLARING RIVER HANDS 2");
4

1 回答 1

4

In the first allocation, you're creating 1214143710 arrays, each of which is 7 bytes plus the object overhead. If we assume a per-object overhead for an array of 16 bytes (which is reasonable, if not conservative) then that means two thirds of your space is being wasted. If we assume 24 bytes for each array in total, that's ~29GB just for the first allocation... and if there's more overhead than that, pushing it to 32 bytes for each array, for example, it's ~38GB, at which point it's not surprising that you can't do it twice :)

Just change the way you're using the arrays to make this much more efficient:

// Names changed to follow Java naming conventions
byte[][] riverHands1 = new byte[7][halfRiver];
byte[][] riverHands2 = new byte[7][(int)(NUM_RIVER_HANDS - halfRiver)];

You'll need to change how you use the arrays as well, but now you have a total of 14 byte[] objects, each of which is very large... but you'll have hardly any wasted memory due to object overheads.

于 2013-02-20T14:55:30.120 回答