Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: Scala 2.10.0, Scala 2.10.1
    • Fix Version/s: Scala 2.10.3-RC1
    • Component/s: Concurrent Library
    • Labels:
      None

      Description

      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]

      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.

        Issue Links

          Activity

          Show
          Adriaan Moors added a comment - https://github.com/scala/scala/pull/2674

            People

            • Assignee:
              Rich Dougherty
              Reporter:
              Stephane Godbillon
            • Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development