diff options
author | Tobias Roeser <le.petit.fou@web.de> | 2019-02-11 10:05:26 +0100 |
---|---|---|
committer | Tobias Roeser <le.petit.fou@web.de> | 2019-02-11 10:16:08 +0100 |
commit | b9de3ec501c8b6046ed9f69625d6f85aa6463a56 (patch) | |
tree | efc6f538c0ed75aa3fd77d7089135bcc614ed01e | |
parent | 671741553c6e753ce863d39b26bb69f501b1e098 (diff) | |
download | mill-b9de3ec501c8b6046ed9f69625d6f85aa6463a56.tar.gz mill-b9de3ec501c8b6046ed9f69625d6f85aa6463a56.tar.bz2 mill-b9de3ec501c8b6046ed9f69625d6f85aa6463a56.zip |
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.
-rw-r--r-- | scalalib/worker/src/ZincWorkerImpl.scala | 21 |
1 files changed, 14 insertions, 7 deletions
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], |