diff options
author | Rich Dougherty <rich@rd.gen.nz> | 2013-07-02 21:13:12 +1200 |
---|---|---|
committer | Rich Dougherty <rich@rd.gen.nz> | 2013-07-06 20:04:52 +1200 |
commit | 48c677ceb3177d93e700b399c00af6b8bb6419e4 (patch) | |
tree | f424315d2fe0fef86290be9266c7f33fd10ee7f3 /test/files/run | |
parent | 54cb6af7dbcf630a4f57e98f0099d77dd3b36693 (diff) | |
download | scala-48c677ceb3177d93e700b399c00af6b8bb6419e4.tar.gz scala-48c677ceb3177d93e700b399c00af6b8bb6419e4.tar.bz2 scala-48c677ceb3177d93e700b399c00af6b8bb6419e4.zip |
SI-7336 - Link flatMapped promises to avoid memory leaks
Diffstat (limited to 'test/files/run')
-rw-r--r-- | test/files/run/t7336.scala | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/test/files/run/t7336.scala b/test/files/run/t7336.scala new file mode 100644 index 0000000000..ace83f2c1f --- /dev/null +++ b/test/files/run/t7336.scala @@ -0,0 +1,31 @@ +import scala.concurrent.Await +import scala.concurrent.ExecutionContext.Implicits.global +import scala.concurrent.Future +import scala.concurrent.duration.Duration + +/** This test uses recursive calls to Future.flatMap to create arrays whose + * combined size is slightly greater than the JVM heap size. A previous + * implementation of Future.flatMap would retain references to each array, + * resulting in a speedy OutOfMemoryError. Now, each array should be freed soon + * after it is created and the test should complete without problems. + */ +object Test { + def main(args: Array[String]) { + def loop(i: Int, arraySize: Int): Future[Unit] = { + val array = new Array[Byte](arraySize) + Future.successful(i).flatMap { i => + if (i == 0) { + Future.successful(()) + } else { + array.size // Force closure to refer to array + loop(i - 1, arraySize) + } + + } + } + + val arraySize = 1000000 + val tooManyArrays = (Runtime.getRuntime().totalMemory() / arraySize).toInt + 1 + Await.ready(loop(tooManyArrays, arraySize), Duration.Inf) + } +}
\ No newline at end of file |