This is a distillation of some generated code we have that is causing problems now that we've switched to 1.8.
Can someone help explain why this compiles and runs in Java 1.6, but causes an Out of Memory Error in 1.8? Also, it seems to run fine in 1.8 if you comment out the set.add(s1)
line.
I'm pretty sure it's not because I'm storing the 5-character substrings in a set. It ought to be able to handle 12,000 of those. Plus, it works in 1.6, even if I change the line to set.add(new String(s1))
or set.add(s1 + " ")
to try and force the creation of new strings.
package put.your.package.here;
import java.util.HashSet;
import java.util.Set;
public class SubstringTest {
public static void main(String[] args) {
String s = buildArbitraryString();
System.out.println(System.getProperty("java.version") + "::" + s.length());
Set<String> set = new HashSet<String>();
while (s.length() > 0) {
s = whackString(s, set);
}
}
private static String whackString(String s, Set<String> set) {
String s1 = s.substring(0, 5);
String s2 = s.substring(5);
s = s2;
set.add(s1);
System.out.println(s1 + " :: " + set.size());
return s;
}
private static String buildArbitraryString() {
StringBuffer sb = new StringBuffer(60000);
for (int i = 0; i < 15000; i++)
sb.append(i);
String s = sb.toString();
return s;
}
}
Any ideas?
JVM Version Info:
java.vm.name=IBM J9 VM
java.fullversion=
JRE 1.8.0 IBM J9 2.8 Windows 7 amd64-64 Compressed References 20160210_289934 (JIT enabled, AOT enabled)
J9VM - R28_Java8_SR2_20160210_1617_B289934
JIT - tr.r14.java_20151209_107110.04
GC - R28_Java8_SR2_20160210_1617_B289934_CMPRSS
J9CL - 20160210_289934
edited to add JVM info