At my current project we are planing to implement special DSL to allow end-user to do workflow customizations. We are considering several ways to do it and one of the them is to use Scala Interpreter (IMain) and DSL written in Scala itself. Doing some initial experiments I've found following simple programm to have memory leaks and leads to full heap consumption. It could be solved by creating new IMain object each time but it is very expensive operation (time and memory) so it's definitely better to use single Interpreter. interpreter.reset method is called each time but it doesn't help.
If somebody have experience with Scala Interpreter could you tell how to use it correctly and avoid memory leaks?
We are using Scala 2.9.
import scala.tools.nsc.interpreter._
import scala.tools.nsc.Settings
import java.util.concurrent.TimeUnit
object DslTest {
val settings = new Settings()
settings.usejavacp.value = true
var interpreter = new IMain(settings);
def main开发者_如何学C(args : Array[String]) {
val t = System.currentTimeMillis
do {
test()
} while (System.currentTimeMillis - t < TimeUnit.SECONDS.toMillis(3000))
}
def test() {
interpreter.interpret("""
def sort(a:Array[Int]): Array[Int] =
if (a.length < 2) a
else {
val pivot = a(a.length / 2)
sort (a filter (pivot>)) ++
(a filter (pivot == )) ++
sort (a filter(pivot <))
}""");
interpreter.reset
}
}
This is not a memory leak. You are constantly adding new sort
definitions for the duration of the while loop, and, naturally, each sort definition will increase memory usage.
See also class unload.
精彩评论