Leon K
02/19/2020, 10:35 AMilya.chernikov
02/19/2020, 1:53 PMeval
call. So, if REPL is not something you need or want, you either can recreate an engine for every eval
or use scripting API instead.Leon K
02/19/2020, 1:54 PMilya.chernikov
02/19/2020, 1:56 PMilya.chernikov
02/19/2020, 1:58 PMThe thing is, I current AM creating a new engine every eval call, but still leaking memory.This is interesting. Do you have more info? The rate of leaking? Maybe a memory snapshot or code sample to analyze?
Leon K
02/19/2020, 2:00 PMilya.chernikov
02/19/2020, 2:02 PMLeon K
02/19/2020, 2:03 PMLeon K
02/19/2020, 2:52 PM-Xmx200m
) I'm quickly running into OutOfMemoryError: GC overhead limit exceeded
with this code:
import org.jetbrains.kotlin.cli.common.environment.setIdeaIoUseFallback
import javax.script.ScriptEngineManager
import kotlin.random.Random
fun main() {
setIdeaIoUseFallback()
val dsl = """
createFamily {
person {
name = "tom"
age = 12
}
}
"""
repeat(10000) {
val scriptEngine = ScriptEngineManager().getEngineByExtension("kts")!!
val family = scriptEngine.eval(dsl) as Family
println(family)
}
}
fun createFamily(build: FamilyBuilder.() -> Unit) = FamilyBuilder().apply(build).create()
data class Family(val people: List<Person>)
class FamilyBuilder() {
private val people = mutableListOf<Person>()
fun person(build: PersonBuilder.() -> Unit) {
people.add(PersonBuilder("", 0).apply(build).create())
}
fun create() = Family(people)
}
data class Person(val name: String, val age: Int) {
// just some large object to speed up garbage-production
private val bigData: ByteArray = Random.Default.nextBytes(1_000_000)
}
class PersonBuilder(var name: String, var age: Int) {
fun create() = Person(name, age)
}
I'm not sure if this should work, but my feeling is that if i create an instance in a loop, print it out and then ignore it, the GC should be able to collect it easily and have no problem here. I know I'm creating a lot of garbage data here, so the "GC overhead limit exceeded" error could just stem from it having to do to much, but I'm also getting the same error in my actual usecase, just a lot slower as there is less garbage created a lot slower.James Richardson
02/25/2020, 8:55 AMLeon K
02/25/2020, 10:38 AM