[英]Space required to keep largest integer array in memory
我正在審查一種算法,它保留一個整數數組,輸入的大小是動態的。 所以根據我的計算,它可能需要多達
integer MAX_VALUE * int size = ?
2^31 integers * 4 bytes = ?
2147483648 integers * 4 bytes = 8 Gigabytes
這個計算正確嗎? JVM 會使用這么多連續的空間來存儲 int 數組還是需要考慮其他事情?
數組的理論大小為:
numberOfElementsInTheArray * 4 字節
12 字節的標頭( int[]
是一個對象)。 實際上,標題的大小取決於您使用的標志以及您正在運行的 JVM 版本
4個字節來保持數組的length
填充。
例如:(我將為此使用JOL ):
int [] x = new int[10];
for(int i=0;i<10;++i){
x[i] = 9999;
}
System.out.println(GraphLayout.parseInstance((Object)x).toPrintable());
將 output:
[I@7a81197dd object externals:
ADDRESS SIZE TYPE PATH VALUE
70fe45268 56 [I [9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999]
所以它有56 bytes
:
如果將此數組更改為Integer
,情況將發生巨大變化。 Integer
is an Object so you will store a reference inside the array (which could be 4
or 8
bytes, depending on UseCompressedOops
flag), plus each of the Integer
instances will require 2 headers (each Integer
is an Object).
Integer[] y = new Integer[10];
for(int i=0;i<10;++i){
y[i] = 9999;
}
System.out.println(GraphLayout.parseInstance((Object)y).toFootprint());
這將顯示:
[Ljava.lang.Integer;@369f73a2d footprint:
COUNT AVG SUM DESCRIPTION
1 56 56 [Ljava.lang.Integer;
10 16 160 java.lang.Integer
11 216 (total)
共216 bytes
:
UseCompressedOop
),總共 40 個字節該數組中的每個引用都指向一個Integer
,每個對象都有16 bytes
:
int
為 4 個字節Integer.MAX_VALUE
不,您的最大值不正確。
The limit on number of elements in an array in Java is a little bit less than Integer.MAX_VALUE
(2,147,483,647), depending on the version of Java, the host OS, and how Java was compiled. 請參閱 Ivan Mamontov 關於問題的這個答案,為什么我不能創建一個大尺寸的數組? .
int
數組 ≈ 8 gigs 因此,最大int
數組的大小將大致為( Integer.MAX_VALUE - 8L ) * 32L
位,即 68,719,476,448 位,即 8,589,934,556 個八位字節。
所以是的,大約 8 場 memory。 請記住:這是一個數組的連續memory。 所以:
如果您真的在工作中突破這些限制,請務必進行實際測試。 您可能需要考慮為非常大的 memory 設計的 Java 的替代實現,例如 Azul Systems 的Zing 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.