Luis Munoz
10/06/2020, 3:41 PMilya.chernikov
10/06/2020, 4:51 PMLuis Munoz
10/06/2020, 6:50 PMval engine = ScriptEngineManager().getEngineByExtension("kts")
assert(engine.eval("true") as Boolean)
val scriptA = """
import eco.analytics.normalize.engine.Ctx
class ObjA : Ctx() {
override fun ping() : String {
return "ObjA"
}
}
val objA = ObjA()
objA
""".trimIndent()
val objA = engine.eval(scriptA) as Ctx
assertThat(objA.ping()).isEqualTo("ObjA")
val scriptB = """
import eco.analytics.normalize.engine.Ctx
class ObjB : Ctx() {
override fun ping() : String {
return "ObjB"
}
}
val objB = ObjB()
objB
""".trimIndent()
val objB = engine.eval(scriptB) as Ctx
assertThat(objB.ping()).isEqualTo("ObjB")
Luis Munoz
10/06/2020, 6:51 PMLuis Munoz
10/06/2020, 6:53 PMilya.chernikov
10/06/2020, 8:22 PMObjA
is visible from the ObjB
.) The size added to this state by every compilation depends on the script size and possible additional imports, but generally shouldn't be very big. But it might be considered a leak in some sense.
Dropping an engine and creating a new one will reset the state, but if you keep instances of the objects, they may retain some links to that state inside - it s passed indirectly on instance creation in order to support some inner machinery of the JSR-223 REPL. So it is something to investigate, but there is a chance that you'll get a real leak in this scenario.
To be able to avoid it you'd need to use a custom scripting host, probably non-REPL one for simplicity. In this case you probably will pay a performance penalty for initializing the compiler for every script compilation, but it will allow you to recycle compiler after the compilation immediately, leaving only evaluation infrastructure (a classloader with dependencies) and the script class instance in memory.
You can find examples of custom scripting hosts here - https://github.com/Kotlin/kotlin-script-examples.Luis Munoz
10/06/2020, 8:39 PM