-
Bug
-
Resolution: Fixed
-
P4
-
None
-
None
-
b19
For example, here is a recursive example from
IntStream.rangeClosed(0, 1).
flatMap(Test::map).
limit(5).
forEach(System.out::println);
static IntStream getChildren() {
return IntStream.rangeClosed(0, 1).
flatMap(Test::map);
}
static IntStream map(int c) {
return IntStream.concat(
IntStream.of(c),
getChildren());
}
which will result in a StackOverlowError.
Here is another example using infinite streams:
IntStream.rangeClosed(i - 1, i + 1).
flatMap(c -> IntStream.generate(() -> 1).
flatMap(x -> IntStream.generate(() -> x))).
limit(5).forEach(System.out::println);
which will result in an OutOfMemoryError.
To solve these issues will require a mechanism to propagate the enclosing stream's cancellation function (Sink::cancellationRequested) to nested streams. Such as a terminal operation forEachOrderedWithCancel that accepts a Predicate function to query the enclosing cancellation state.
This will also avoid an issue with the spliterator obtained from a stream (AbstractWrappingSpliterator and sub-classes) which has to buffer elements to avoid reporting more than one element via Spliterator::tryAdvance (independently the implementation can be improved by directly reporting the first element and buffering subsequent elements).
- duplicates
-
JDK-8075939 Stream.flatMap() causes breaking of short-circuiting of terminal operations
- Resolved
-
JDK-8189234 Unexpected StackOverflowError when using 'limit' on infinite list generated with recursive 'flatMap' call
- Closed
- relates to
-
JDK-8202307 Getting a java.lang.OutOfMemoryError: Java heap space when calling Stream.iterator().next() on a stream which uses an infinite/very big Stream in flatMap.
- Closed