Skip to content

Commit f84acc9

Browse files
committed
SI-7266 Stream leaks memory
Changed tail-generation function to mutable and clear it after it's used to allow any captured memory to be freed once the tail has been generated. (This is a case where a by-name parameter was used when a lazy val parameter was wanted instead. If we ever get lazy val parameters, we should switch to that.)
1 parent 624e668 commit f84acc9

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/library/scala/collection/immutable/Stream.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,11 +1108,15 @@ object Stream extends SeqFactory[Stream] {
11081108
override def isEmpty = false
11091109
override def head = hd
11101110
@volatile private[this] var tlVal: Stream[A] = _
1111-
def tailDefined: Boolean = tlVal ne null
1111+
@volatile private[this] var tlGen = tl _
1112+
def tailDefined: Boolean = tlGen eq null
11121113
override def tail: Stream[A] = {
11131114
if (!tailDefined)
11141115
synchronized {
1115-
if (!tailDefined) tlVal = tl
1116+
if (!tailDefined) {
1117+
tlVal = tlGen()
1118+
tlGen = null
1119+
}
11161120
}
11171121

11181122
tlVal

0 commit comments

Comments
 (0)