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.
The text was updated successfully, but these errors were encountered:
Following up this thread: https://groups.google.com/forum/?fromgroups=#!topic/play-framework-dev/58VZD-YXdJw
Using a lot of flatMap calls leaks. Here is a snippet of code that shows the problem:
import scala.concurrent._
import scala.concurrent.duration._
import ExecutionContext.Implicits.global
import scala.util._
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]
}
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.
The text was updated successfully, but these errors were encountered: