Using heap size with "javaw.exe" versus "java.exe"

Below is the program,

public class Dummy {

    public static void main(String[] args) throws Exception {
        final int LENGTH = Integer.MAX_VALUE / 8;
        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++;
            }
        }
    }
}

      

when running through eclipse (after 64 bit javaw.exe

) heap usage is used which gradually approaches the given approximate value (max) and the battery goes out in minutes,

enter image description here

and then shows the following exception:

Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded

      

On the same machine, when started using 64-bit java.exe

from the command line, the new hash code sequentially collides with the previous hash code after creating 22985 objects for-loop

with a "private working set" of 1GB (max).

:
23206321
39915397
found after 22985: java.lang.Object@f2eb847 same hashcode as java.lang.Object@f2eb847
false

      

Without concentrating on the logic of the code, I would like to understand,

1) Why does the difference in heap usage compare both approaches? Because there is no customization for any approach.

2) How do I manage the heap usage options before running the program, either through eclipse ( javaw.exe

) or command line ( java.exe

)? Please help me!

Note: I am working with java 1.6

+3


source to share


2 answers


unless you specify that the JVM uses Ergonomics (a solution to set defaults) based on the host architecture and sets various default options for the JVM, the heap is one of them

for 64bit processor JVM sets higher heap value and so you see latency in OOM

You can check this by calling

java -XX:+PrintFlagsFinal -version 2>&1 | grep MaxHeapSize

      



Since you are using windows you can use some JDK tools or you can use this program to check the default memory setting

long maxBytes = Runtime.getRuntime().maxMemory();
System.out.println("Max memory: " + maxBytes / 1024 / 1024 + "M");

      

in both cars

you can also override the heap size by explicitly specifying one in this case, you should see similar behavior with memory perspective

+6


source


Why does the difference in heap usage compare both approaches? Because there is no customization for any approach.

Your test is based on random number generation i.e. hash code. This means that it will run for some time before stopping. If you print the hash codes, you will see that they are 31-bit (none negative) even on a 64-bit machine, and they are randomly ordered. that is, they have nothing to do with address locations, and they cannot be changed no matter where the object is in memory.



How do I manage the heap usage options before running the program, either through eclipse (javaw.exe) or the command line (java.exe)?

You can manage memory running in eclipse by changing command line argument options in eclipse. On the command line, specify the amount of memory required.

0


source







All Articles