其实java里面的集合,数组等他们的存储容量我们是可以预估的。1、存储原始类型,这种存储大小是固定的。2、存储对象,由于内容大小有变化,但是我们可以通过测试获取到大慨存储容量。
还有就是我们扩容内存,会影响集合或数组存储大小。
1、List<Integer>
int judgeListmaxCount = 0;
try {}
异常消息:
Exception in thread "main" judgeListmaxCount:38647475
java.lang.OutOfMemoryError: Java heap space
at java.util.ArrayList.ensureCapacity(ArrayList.java:169)
at java.util.ArrayList.add(ArrayList.java:351)
at com.yxie.test.CreateDichotoSearchData.main(CreateDichotoSearchData.java:21)
xieyun@xieyun-OptiPlex-760:~/work/dichotomy/bin$ java -Xms1024m -Xmx1024m com.yxie.test.CreateDichotoSearchData
judgeListmaxCount:38647475
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2760)
at java.util.Arrays.copyOf(Arrays.java:2734)
at java.util.ArrayList.ensureCapacity(ArrayList.java:167)
at java.util.ArrayList.add(ArrayList.java:351)
at com.yxie.test.CreateDichotoSearchData.main(CreateDichotoSearchData.java:21)
2、int[]
int intSize = 0;
try {
for (int i = 191059999; i <= 191160000; i++) {
intSize = i;
int[] allInt = new int[i];
System.out.println("allInt:" + allInt.length);
}
} catch (Exception e) {
} finally {
System.out.println("intSize:" + intSize);
}
异常消息:
intSize:191060000
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at com.yxie.test.CreateDichotoSearchData.main(CreateDichotoSearchData.java:54)
改变java虚拟机内存大小,会影响int[]数组大小。
xieyun@xieyun-OptiPlex-760:~/work/dichotomy/bin$ java -Xms1024m -Xmx1024m com.yxie.test.CreateDichotoSearchData
Integer max:2147483647
fileSize:4000000000
spareFileSize:1852516353
intSize:191059999
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at com.yxie.test.CreateDichotoSearchData.main(CreateDichotoSearchData.java:54)
xieyun@xieyun-OptiPlex-760:~/work/dichotomy/bin$
xieyun@xieyun-OptiPlex-760:~/work/dichotomy/bin$ java -Xms2024m -Xmx2024m com.yxie.test.CreateDichotoSearchData
Integer max:2147483647
fileSize:4000000000
spareFileSize:1852516353
allInt:191059999
allInt:191060000
allInt:191060001