Skip to content

Commit

Permalink
Cleanup after tests to avoid OOME: Metaspace in fast-run testOnly seq…
Browse files Browse the repository at this point in the history
…uence

After these changes, this project no longer exhibits the problems
discussed in sbt/sbt#2056
  • Loading branch information
retronym committed Jul 13, 2018
1 parent 99e6812 commit dd7b74a
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ def commonSettings: Seq[Setting[_]] = Def.settings(
inCompileAndTest(scalacOptions in console --= Vector("-Ywarn-unused-import", "-Ywarn-unused", "-Xlint")),
publishArtifact in Compile := true,
publishArtifact in Test := false,
parallelExecution in Test := false
parallelExecution in Test := false,
testOptions in Test += {
val log = streams.value.log
Tests.Cleanup { loader => cleanupTests(loader, log) }
}
)

val mimaSettings = Def settings (
Expand Down Expand Up @@ -275,3 +279,22 @@ inThisBuild(Seq(

def inCompileAndTest(ss: SettingsDefinition*): Seq[Setting[_]] =
Seq(Compile, Test) flatMap (inConfig(_)(Def.settings(ss: _*)))


// TODO move into sbt-house-rules?
def cleanupTests(loader: ClassLoader, log: sbt.internal.util.ManagedLogger): Unit = {
// shutdown Log4J to avoid classloader leaks
try {
val logManager = Class.forName("org.apache.logging.log4j.LogManager")
logManager.getMethod("shutdown").invoke(null)
} catch {
case _: Throwable =>
log.warn("Could not shut down Log4J")
}
// Scala Test loads property bundles, let's eagerly clear then from the internal cache
// TODO move into SBT itself?
java.util.ResourceBundle.clearCache(loader)
// Scala Test also starts TimerThreads that it doesn't eagerly cancel. This can weakly retain
// metaspace until a full GC.
System.gc()
}

0 comments on commit dd7b74a

Please sign in to comment.