Reputation: 1421
The way I understand view structure in scala is that view is a lazy collection and is not filled with real collection members before they are accessed. With this description of view, the following line of code should not throw an error:
val a = (0 to 123456789).view
and as expected it does not. I (most probably wrongly) assume the following line of code is equivalent to the above code and should not throw an out of memory error either:
val a = Array.range(0,123456789).view
but it generates a “java.lang.OutOfMemoryError: Java heap space” error. Any idea why these two lines of code behave differently?
Upvotes: 0
Views: 853
Reputation: 3072
Let's break it up...
val a1: Range.Inclusive = (0 to 123456789)
val a2: AnyRef with SeqView[Int, IndexedSeq[Int]] = a1.view
val b1: Array[Int] = Array.range(0,123456789)
val b2: AnyRef with mutable.IndexedSeqView[Int, Array[Int]] = b1.view
IntelliJ inferred (press Ctr-Q) that b1 is an array.
Let's check the docs for "Array.range"...
/** Returns an array containing a sequence of increasing integers in a range.
*
* @param start the start value of the array
* @param end the end value of the array, exclusive (in other words, this is the first value '''not''' returned)
* @return the array with values in range `start, start + 1, ..., end - 1`
* up to, but excluding, `end`.
*/
def range(start: Int, end: Int): Array[Int] = range(start, end, 1)
The error is coming from the line where you call Array.range
...
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at scala.collection.mutable.ArrayBuilder$ofInt.mkArray(ArrayBuilder.scala:323)
at scala.collection.mutable.ArrayBuilder$ofInt.resize(ArrayBuilder.scala:329)
at scala.collection.mutable.ArrayBuilder$ofInt.sizeHint(ArrayBuilder.scala:334)
at scala.Array$.range(Array.scala:402)
at scala.Array$.range(Array.scala:390)
at pkg.Main$.main(Main.scala:57)
at pkg.Main.main(Main.scala)
You just allocated an array with 123456790 elements in it, which is why you got a “java.lang.OutOfMemoryError: Java heap space” error.
Upvotes: 3