Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream.tails leaks memory #10131

Closed
scabug opened this issue Jan 2, 2017 · 7 comments · Fixed by scala/scala#9098
Closed

Stream.tails leaks memory #10131

scabug opened this issue Jan 2, 2017 · 7 comments · Fixed by scala/scala#9098
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Jan 2, 2017

this code causes OOME

  def stream = Stream.continually(new Array[Byte](1024 * 1024))
  stream.tails.take(100000).foreach{_ => }

generated Iterator has reference to underlying stream via calling newBuilder
https://github.com/scala/scala/blob/v2.12.1/src/library/scala/collection/TraversableLike.scala#L806

@scabug
Copy link
Author

scabug commented Jan 2, 2017

Imported From: https://issues.scala-lang.org/browse/SI-10131?orig=1
Reporter: seraph (seraphr)
Affected Versions: 2.11.8, 2.12.1

@szeiger
Copy link
Member

szeiger commented Nov 9, 2018

Moving to backlog because Stream is deprecated.

@szeiger szeiger added this to the Backlog milestone Nov 9, 2018
@NthPortal
Copy link

LazyList does not OOM here.

Stream still does this in 2.13 M5.

@counter2015
Copy link

LaszList in 2.13.2 causes OOME in my test environment

Welcome to Scala 2.13.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_181).
Type in expressions for evaluation. Or try :help.

scala> def stream = LazyList.continually(new Array[Byte](1024 * 1024))
def stream: scala.collection.immutable.LazyList[Array[Byte]]

scala> stream.tails.take(100000).foreach{_ => }
java.lang.OutOfMemoryError: Java heap space
  at $anonfun$stream$1(<console>:1)
  at $Lambda$992/447477005.apply(Unknown Source)
  at scala.collection.immutable.LazyList$.$anonfun$continually$1(LazyList.scala:1198)
  at scala.collection.immutable.LazyList$$$Lambda$998/1867108691.apply(Unknown Source)
  at scala.collection.immutable.LazyList.scala$collection$immutable$LazyList$$state$lzycompute(LazyList.scala:230)
  at scala.collection.immutable.LazyList.scala$collection$immutable$LazyList$$state(LazyList.scala:223)
  at scala.collection.immutable.LazyList.isEmpty(LazyList.scala:240)

@NthPortal
Copy link

you are correct; my apologies. assigning to myself to fix the LazyList portion

@NthPortal NthPortal self-assigned this Jul 2, 2020
@NthPortal NthPortal modified the milestones: Backlog, 2.13.4 Jul 2, 2020
@NthPortal
Copy link

NthPortal commented Jul 2, 2020

arguably this is a problem with Iterator.iterate, in that it closes over the start parameter it does not do this

@NthPortal
Copy link

NthPortal commented Jul 2, 2020

update: in actuality, it is the implementation of tails in LinearSeqOps that is broken. the implementation is as follows:

  override def tails: Iterator[C] =
    Iterator.iterate(coll)(_.tail).takeWhile(_.nonEmpty) ++ Iterator.single(newSpecificBuilder.result())

the last element in the iterator is lazy, but invokes newSecificBuilder on the initial collection. thus, it retains a reference to the head of the collection, and leaks memory.

the fix for this problem is to eagerly create the Iterator.single, and it fixes both LazyList and Stream. alternatively, you could make the implementation track whether the previous element was empty, and then you don't need to concat the iterators at all, but that seems less efficient

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants