How to create more than Integer.MAX_VALUE objects in a jvm?
在此讨论中,我需要编写一个程序来知道问题的答案:"如果对象多于int的maxvalue,那么jvm将什么分配给对象以地址?",
在eclipse.ini中具有以下设置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | -startup plugins/org.eclipse.equinox.launcher_1.3.0.v20130327-1440.jar --launcher.library plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.200.v20131025-1931 -product org.eclipse.epp.package.jee.product --launcher.defaultAction openFile --launcher.XXMaxPermSize 256M -showsplash org.eclipse.platform --launcher.XXMaxPermSize 256m --launcher.defaultAction openFile --launcher.appendVmargs -vmargs -Dosgi.requiredJavaVersion=1.6 -Xms6144m -Xmx6144m |
及以下程序,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | class SubClass { int x = 4; int getX(){ return x; } } public class Dummy2 { public static void main(String[] args){ SubClass obj[] = null; obj = new SubClass[Integer.MAX_VALUE]; for(int i = 0; i < Integer.MAX_VALUE; i++){ obj[i] = new SubClass(); } SubClass objRef1 = new SubClass(); System.out.println(objRef1); System.out.println(objRef1.hashCode()); SubClass objRef2 = new SubClass(); System.out.println(objRef2); System.out.println(objRef2.hashCode()); } } |
我收到错误消息:
1 |
我的目标是查看对象地址的值以及为最后两个对象生成的哈希码。
Am使用在64位Windows 7操作系统上运行的64位jvm,该虚拟机为每个本机用户级别的进程提供8TB的虚拟空间。硬件具有8GB RAM芯片。
请帮助我!
这里有很多误解。
- Object.hashCode()是一个随机生成的数字,它存储在对象的标题中,并且在对象移动时不会改变。
- Windows不允许JVM使用虚拟内存,您不能创建大于主内存的JVM堆。
- 您至少需要20 GB的JVM内存才能容纳20亿个元素。
- 一个数组不能超过Integer.MAX_VALUE个元素,但是您可以拥有一个数组数组(或多个数组)
My goal is to see the value of object's internal address and the hashcode generated for last two objects.
它们是随机的,因此它们与前几个元素之间没有区别。
我假设您是在问这个问题。
您不必创建多个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public static void main(String[] args) throws Exception { final int LENGTH = Integer.MAX_VALUE / 256; Object[] values = new Object[LENGTH]; int count = 0; for (int i = 0; i < Integer.MAX_VALUE; i++) { Object o = new Object(); int hashCode = o.hashCode(); if (hashCode > LENGTH) continue; if (values[hashCode] != null) { System.out.println("found after" + count +":" + values[hashCode] +" same hashcode as" + o); System.out.println(values[hashCode] == o); System.exit(0); } else { System.out.println(hashCode); values[hashCode] = o; count++; } } } |
例如,在我的计算机上,此操作在4712个新实例(
上面的印刷品
1 2 3 4 5 6 |