I get an OOM in Java 19 even with 8GB if I do this:
IntStream
.iterate(0, i -> i + 1)
.skip(2)
.limit(10_000_000)
.filter(i -> checkSum(i) <= 20)
.parallel()
.count();
However, I don't get any OOM if I omit skip(2):
IntStream
.iterate(0, i -> i + 1)
//.skip(2)
.limit(10_000_000)
.filter(i -> checkSum(i) <= 20)
.parallel()
.count();
where checksum(...) is
public static long checkSum(long n) {
long result = 0;
long remaining = n;
while (0 < remaining) {
long remainder = remaining % 10;
result += remainder;
remaining = (remaining - remainder) / 10;
}
return result;
}
Why does skip() in this parallel stream expression in Java 19 cause an OOM even with 8GB?
I know I should use range(...) instead of iterate()+limit() with or without skip(). However, that doesn't answer me this question. I would like to understand what's the issue here.