You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
object FutureLeak extends App {
val step = 100000
val upper = 1000000
// code that leaks
def loop(future: Future[Int]): Future[Int] = {
future.flatMap { i =>
if (i % step == 0) println(i)
if (i < upper)
loop(Future(i + 1))
else Future(i)
}
}
// code that does not leak
def loop2(future: Future[Int]): Future[Int] = {
val promise = Promise[Int]
def inloop(future: Future[Int]): Unit = {
future.onComplete {
case Success(i) if i < upper =>
if (i % step == 0) println(i)
inloop(Future(i + 1))
case Success(i) =>
println("done with " + i)
promise.success(i)
}
}
inloop(future)
promise.future
}
Await.result(loop(Future(0)), 100 seconds) // leads to java.lang.OutOfMemoryError: Java heap space with -Xms32m -Xmx32m
Await.result(loop2(Future(0)), 100 seconds) // runs fine with -Xms32m -Xmx32m
}
It should not leak since the produced futures do not depend on each other.
Activity
scabug commentedon Apr 5, 2013
Imported From: https://issues.scala-lang.org/browse/SI-7336?orig=1
Reporter: Stephane Godbillon (sgodbillon)
Affected Versions: 2.10.0, 2.10.1
See #7493
scabug commentedon Jul 12, 2013
@adriaanm said:
scala/scala#2674