Kotlin Script Engine Memory Leak

Hello!
I wrote a small test application, that execute a simple script (just print a line (println (“test”))).
I looped on the execution of this script. And after several thousand iterations (~ 10 000) the application fell with OutOfMemory.

Sample code

System.setProperty("idea.io.use.fallback", "true")
val e2 = KotlinScriptEngineFactory().scriptEngine
var i = 0
while (i<100_000_000) {
    println("i = $i")
    val bindings = SimpleBindings()
    bindings["srv"] = TestService();
    e2.eval("""
    println("test")
""".trimIndent(), bindings)
    i++

}

What am I doing wrong? may need to call some resource freeing method?
Nobody stalled with memory leaks when using a cotlin script engine?
Sorry for my English

4 Likes

Thanks for the report, I submitted an issue at https://youtrack.jetbrains.com/issue/KT-32020, please follow it for updates.

1 Like

I was trying this as well, within the context of a Spring Boot web-app and have tried so many things. Even if I try creating a new Engine from the factory for each eval, I eventually run out of memory under load – there is a leak somewhere. I may be doing something a bit unique. I have a directory on the file-system that another team can upload custom scripts to. Within the context of an HTTP request to a Spring Boot app, I look up the script dynamically off the file-system based on some keys passed into the API and I eval that script. I’ve tried to debug this – going down into the REPL contexts and class loaders – but really struggling. If you can point me to any tips, I’d be GREATLY appreciated!

I also want to report about this leak. I’m currently executing only 1 kts-script with one line of code (log.info(“hello world”)) and i run into the OutOfMemory after < 100 executions. I do not have the experience on kotlin to debug this on my own. I can only report that i also added JavaScript and Jython ScriptEngines in our framework and with the same code i can execute thousands of scripts without any issue. Would be nice if there is a solution other than having to stop the JVM entirely to get rid of the garbage objects.

I guess you should open a new ticket as it may be a different kind of leak. It works better if you post a simple example that fails.

Serious problem, ignored with a poor excuse, and left unfixed for 2 years… That’s disappointing.