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