summaryrefslogtreecommitdiff
path: root/test/files
diff options
context:
space:
mode:
authorTim Harper <timcharper@gmail.com>2014-10-29 00:15:43 -0600
committerTim Harper <timcharper@gmail.com>2014-11-22 22:22:26 -0700
commitd367c5f925e7055e9ee5b4071328e92cf48ce0ed (patch)
tree91c45f1147b28992209ae47ee0394a08e3370c0d /test/files
parentc4df20d29a8d15ef23cf0d10fad56da0791bbbf6 (diff)
downloadscala-d367c5f925e7055e9ee5b4071328e92cf48ce0ed.tar.gz
scala-d367c5f925e7055e9ee5b4071328e92cf48ce0ed.tar.bz2
scala-d367c5f925e7055e9ee5b4071328e92cf48ce0ed.zip
Fixes memory leak when using reflection
References to Threads would be retained long after their termination if reflection is used in them. This led to a steady, long memory leak in applications using reflection in thread pools.
Diffstat (limited to 'test/files')
-rw-r--r--test/files/run/t8946.scala29
1 files changed, 29 insertions, 0 deletions
diff --git a/test/files/run/t8946.scala b/test/files/run/t8946.scala
new file mode 100644
index 0000000000..a248a20501
--- /dev/null
+++ b/test/files/run/t8946.scala
@@ -0,0 +1,29 @@
+// Tests to assert that references to threads are not strongly held when scala-reflection is used inside of them.
+object Test {
+ import scala.ref.WeakReference
+
+ def forceGc() = {
+ var obj = new Object
+ val ref = new WeakReference(obj)
+ obj = null;
+ while(ref.get.nonEmpty)
+ Array.ofDim[Byte](16 * 1024 * 1024)
+ }
+
+ def main(args: Array[String]): Unit = {
+ val threads = for (i <- (1 to 16)) yield {
+ val t = new Thread {
+ override def run(): Unit = {
+ import reflect.runtime.universe._
+ typeOf[List[String]] <:< typeOf[Seq[_]]
+ }
+ }
+ t.start()
+ t.join()
+ WeakReference(t)
+ }
+ forceGc()
+ val nonGCdThreads = threads.filter(_.get.nonEmpty).length
+ assert(nonGCdThreads == 0, s"${nonGCdThreads} threads were retained; expected 0.")
+ }
+}