简体   繁体   中英

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?",

With the below setting in 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. Hardware has 8GB RAM chip.

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.
  • Windows doesn't allow the JVM to use virtual memory, you can't create a JVM heap which is larger than main memory.
  • you need at least 20 GB of JVM memory for 2 billion elements.
  • An array can't have more than Integer.MAX_VALUE elements, but you can have an array of arrays (or arrays, of arrays)

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. 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 . This means that two Object instances, which weren't eligible for GC, had the same hashCode .

The above prints

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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