In Scala 2.9.1, this works fine:
scala> (1 to Int.MaxValue).sum
res6: Int = -1073741824
Yet this runs out of heap space:
scala> (1 to Int.MaxValue).toIterator.sum
java.lang.OutOfMemoryError: GC overhead limit exceeded
But maddeningly, this works:
scala> (1 to Int.MaxValue).iterator.sum
res8: Int = -1073741824
Why should any of those be different?
toIterator
is defined in TraversableLike
as
def toIterator: Iterator[A] = toStream.iterator
so it creates a Stream
in the background which keeps all elements in memory while iterating.
(Edit: I think the stream structure isn’t the problem here actually. However, toStream
itself calls toBuffer
which in turn copies every single value.)
iterator
on the other hand is defined in IndexedSeqLike
which uses a specialised structure which does not keep any elements in memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With