简体   繁体   English

如何在jvm中创建多个Integer.MAX_VALUE对象?

[英]How to create more than Integer.MAX_VALUE objects in a jvm?

From this discussion i need to write a program to know the answer to question:"if there are more objects than maxvalue of int, then what does jvm assign as an address to an object?", 从这个讨论中,我需要编写一个程序来知道问题的答案:“如果对象多于int的maxvalue,那么jvm将什么分配给对象地址?”,

With the below setting in eclipse.ini 使用eclipse.ini中的以下设置

-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

and below program, 及以下程序,

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());




        }

    }

am getting error: 出现错误:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

My goal is to see the value of object's address and the hashcode generated for last two objects. 我的目标是查看对象地址的值以及为最后两个对象生成的哈希码。

Am using 64 bit jvm running on 64 bit Windows 7 OS which provides virtual space of 8TB for each native user level process. 我正在使用在64位Windows 7操作系统上运行的64位jvm,该操作系统为每个本机用户级别的进程提供8TB的虚拟空间。 Hardware has 8GB RAM chip. 硬件具有8GB RAM芯片。

Please help me!! 请帮我!!

There is a number of misconceptions here. 这里有很多误解。

  • The Object.hashCode() is a randomly generated number which is storing in the object's header and it doesn't change when the object moves. Object.hashCode()是随机生成的数字,存储在对象的标头中,并且在对象移动时不会改变。
  • Windows doesn't allow the JVM to use virtual memory, you can't create a JVM heap which is larger than main memory. Windows不允许JVM使用虚拟内存,您不能创建大于主内存的JVM堆。
  • you need at least 20 GB of JVM memory for 2 billion elements. 您至少需要20 GB的JVM内存才能容纳20亿个元素。
  • An array can't have more than Integer.MAX_VALUE elements, but you can have an array of arrays (or arrays, of arrays) 一个数组不能超过Integer.MAX_VALUE个元素,但是您可以拥有一个数组数组(或多个数组)

My goal is to see the value of object's internal address and the hashcode generated for last two objects. 我的目标是查看对象内部地址的值以及为最后两个对象生成的哈希码。

They are random so there is not difference between them and the first elements. 它们是随机的,因此它们与第一个元素之间没有区别。

I'm assuming you're asking in regards to this . 我想你是在问这个问题

You don't necessarily need to create more than Integer.MAX_VALUE objects. 您不必创建多个Integer.MAX_VALUE对象。 You just need to create some until there is a collision. 您只需要创建一些,直到发生碰撞为止。 You can do that relatively easily. 您可以相对轻松地做到这一点。

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++;
        }
    }
}

For example, on my machine, this stops after 4712 new instances with a hashCode smaller than Integer.MAX_VALUE / 256 . 例如,在我的计算机上,此操作在4712个新实例且其hashCode小于Integer.MAX_VALUE / 256之后停止。 This means that two Object instances, which weren't eligible for GC, had the same hashCode . 这意味着两个不符合GC条件的Object实例具有相同的hashCode

The above prints 以上印刷品

...
5522036
4166797
5613746
found after 4712: java.lang.Object@6aca04 same hashcode as java.lang.Object@6aca04
false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM