From b9de3ec501c8b6046ed9f69625d6f85aa6463a56 Mon Sep 17 00:00:00 2001 From: Tobias Roeser Date: Mon, 11 Feb 2019 10:05:26 +0100 Subject: Only hold weak references to classloaders in internal cache That way, we do not prevent class loader unloading / garbage collection. Also, we reduce the chance to use an outdated class loader. --- scalalib/worker/src/ZincWorkerImpl.scala | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'scalalib') diff --git a/scalalib/worker/src/ZincWorkerImpl.scala b/scalalib/worker/src/ZincWorkerImpl.scala index 7914a10a..a6360d8f 100644 --- a/scalalib/worker/src/ZincWorkerImpl.scala +++ b/scalalib/worker/src/ZincWorkerImpl.scala @@ -3,6 +3,8 @@ package mill.scalalib.worker import java.io.File import java.util.Optional +import scala.ref.WeakReference + import mill.api.Loose.Agg import mill.api.{KeyedLockedCache, PathRef} import xsbti.compile.{CompilerCache => _, FileAnalysisStore => _, ScalaInstance => _, _} @@ -197,18 +199,23 @@ class ZincWorkerImpl(compilerBridge: Either[ } // for now this just grows unbounded; YOLO - val classloaderCache = collection.mutable.LinkedHashMap.empty[Long, ClassLoader] + // But at least we do not prevent unloading/garbage collecting of classloaders + private[this] val classloaderCache = collection.mutable.LinkedHashMap.empty[Long, WeakReference[ClassLoader]] def getCachedClassLoader(compilersSig: Long, combinedCompilerJars: Array[java.io.File]) - (implicit ctx: ZincWorkerApi.Ctx)= { - classloaderCache.synchronized{ - classloaderCache.getOrElseUpdate( - compilersSig, - mill.api.ClassLoader.create(combinedCompilerJars.map(_.toURI.toURL), null) - ) + (implicit ctx: ZincWorkerApi.Ctx) = { + classloaderCache.synchronized { + classloaderCache.get(compilersSig) match { + case Some(WeakReference(cl)) => cl + case _ => + val cl = mill.api.ClassLoader.create(combinedCompilerJars.map(_.toURI.toURL), null) + classloaderCache.update(compilersSig, WeakReference(cl)) + cl + } } } + private def withCompilers[T](scalaVersion: String, scalaOrganization: String, compilerClasspath: Agg[os.Path], -- cgit v1.2.3