4

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 launched via eclipse(thru 64 bit javaw.exe) has the heap usage that goes upto below shown approximate value(max) consistently and battery goes down in minutes,

enter image description here

and then shows the below exception:

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

On same machine, the same program when launched using 64-bit java.exe from command line, new hashcode clashes with previous hashcode consistently after creating 22985 objects in for-loop with "private working set" value of 1GB(max).

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

Without concentrating much on code logic, I would like to understand,

1) Why the difference in heap usage comparing both approaches? Because there is no tuning done for either approach.

2) How do i control heap usage parameters before starting the program either via eclipse(javaw.exe) or via command line(java.exe)? Please help me!!!

Note: am working with java 1.6

overexchange
  • 15,768
  • 30
  • 152
  • 347

2 Answers2

6

if you don't specify JVM uses Ergonomics (a decision to set default values) based on host architecture and it sets various default param for JVM, heap is one of them

for 64bit CPU JVM sets higher value of heap and so you see delay in OOM

You can verify this by invoking

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

Since you are using windows you can either use some JDK tools or you can use this program to verify memory default tuning

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

in both of the machine

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

jmj
  • 237,923
  • 42
  • 401
  • 438
  • how do i override the heapsize? – overexchange Dec 27 '14 at 04:50
  • 1
    by passing `-XmxN` parameter, for example: `-Xmx1g` tries to set 1g for heap http://stackoverflow.com/questions/5374455/what-does-java-option-xmx-stand-for – jmj Dec 27 '14 at 04:51
  • i mean, how do i override heapsize for `javaw.exe` that gets launched via eclipse, because if i set in eclipse.ini, the memory is given to eclipse.exe as per my observation – overexchange Dec 27 '14 at 04:56
  • `eclipse.ini` sets values for eclipse's JVM, it has no effect on the program you launch through eclipse – jmj Dec 27 '14 at 04:57
  • my question is, how do i override heapusage of `javaw.exe` that gets launched via `eclipse.exe`? – overexchange Dec 27 '14 at 05:02
  • 2
    set JVM parameter in launch configuration, Run - > Run configuration > VM Parameters – jmj Dec 27 '14 at 05:06
  • I did the same with parameters "-Xms20g -Xmx20g", i see the msg: `Error occurred during initialization of VM-- Could not reserve enough space for object heap` it works when parameters are set as "-Xms10g -Xmx10g". this machine is 64 bit running with 8GB RAM chip. So, Windows OS allows max virtual user space "8TB" per native process. Please help me!! – overexchange Dec 27 '14 at 06:04
  • ofcourse you can't have 20g of heap for 8g physical memory, I doubt about 10g too, what is your question at this point now ? – jmj Dec 27 '14 at 06:34
  • i need 20g because i need more heap space. the program works with 10g and "runs out of memory". i think there is no link between physical RAM chip size and heap space. please go thru x86 architecture – overexchange Dec 27 '14 at 06:40
  • your originial question has been answered, if you want 20g of heap with 10g of physical memory that is different question – jmj Dec 27 '14 at 06:45
  • the memory that we are allocating with this switch `-Xmx` is NOT physical memory but it is virtual memory. User space application(java/javaw) do not have control over tuning physical memory. As i said please read x86 arch – overexchange Dec 27 '14 at 06:58
  • I don't think here it has anything to do with JVM, it is now operating system question, please ask another question on superusers.com – jmj Dec 27 '14 at 07:10
  • @overexchange Windows eagerly allocates memory, this means that to create a JVM of size X you need at least that much free memory and swap space. note: the GC assumes it has random access memory and will run very slow if even 1% of the heap is on disk. On windows this usually results in you needing to power cycle the machine. – Peter Lawrey Dec 27 '14 at 10:09
0

Why the difference in heap usage comparing both approaches? Because there is no tuning done for either approach.

Your test is based on a random number generation i.e. the hashCode. This means it will run for a random amount of time before stopping. If you print of the hashCodes, you will see they are 31-bit (none are negative) even on a 64-bit machine and they are randomly arranged. i.e. they have nothing to do with address locations, nor should they are they cannot change regardless of where the object is in memory.

How do i control heap usage parameters before starting the program either via eclipse(javaw.exe) or via command line(java.exe)?

You can control the memory started in eclipse by changing the command line arguments setting in eclipse. On the command line by specifying the amount of memory you want.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130