KotlinJsr223JvmLocalScriptEngine doesn't get fully garbage collected

When I used kotlin script engine, I found, that it’s not getting fully garbage collected.

Here is my code:
import javax.script.ScriptEngineManager

fun main() {
for (i in 1..20) {
    val engine = ScriptEngineManager().getEngineByExtension("kts")

    engine.eval("println(\"$i\")")
}

while (true) {
    /*I took memory dump here*/
}
}

There is 20 non collected MockProject objects in heap. I also tried to insert System.gc() before endless loop, but still I can see this MockProject objects. If I increase loop count, and set -Xmx128m option, program eventually fails with
WARN: Could not read file: /home/dzmitry/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.4.10/ea29e063d2bbe695be13e9d044dcfb0c7add398e/kotlin-stdlib-1.4.10.jar!/kotlin/collections/ArraysKt___ArraysKt.class; size in bytes: 703824; file type: CLASS Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

I think, it may be related with memory leak ticket, where @ilya.chernikov suggested getting a new engine for every eval in comments. However, I decided to post here, because the original poster complaining about memory consumption in single script engine, and in case I missed something.

Memory dump screenshot:

Simple github project to reproduce

1 Like

Thank you very much for the detailed report.
It will be very nice to know that retains references to the MockProject objects. If you have the setup at hands, could you please check. Otherwise I’ll do it myself later.

Please note that it is more reliable to leave such reports in YouTrack. If you think that mentioned issue is not 100% relevant, feel free to open a new one. The information will not be lost even we’ll close an issue as a duplicate. Here on forums there is higher chance of it to be lost.

1 Like

So I followed how-to-identify-holder-of-reference answer and what I got is:

Class Name|   Objects | Dom. Objects | Shallow Heap | Dom. Shallow Heap
------------------------------------------------------------------------
          |           |              |              |                  
<ROOT>    |         1 |           20 |            0 |               960
------------------------------------------------------------------------

Output of ‘merge shortest path to garbage collector with all references’ is:

> Class Name                                                                                    | Ref. Objects | Shallow Heap | Ref. Shallow Heap | Retained Heap
> ----------------------------------------------------------------------------------------------------------------------------------------------------------------
>                                                                                               |              |              |                   |              
> class jdk.internal.loader.ClassLoaders @ 0x7076396e0 System Class                             |            1 |           24 |                48 |           112
> '- APP_LOADER jdk.internal.loader.ClassLoaders$AppClassLoader @ 0x70764de00                   |            1 |          104 |                48 |     1,915,352
>    '- classes java.util.Vector @ 0x70763e5c0                                                  |            1 |           32 |                48 |     1,316,784
>       '- elementData java.lang.Object[10240] @ 0x708314430                                    |            1 |       40,976 |                48 |     1,316,752
>          '- [1134] class org.jetbrains.kotlin.com.intellij.openapi.util.Disposer @ 0x707c3daf0|            1 |           16 |                48 |        29,976
>             '- ourTree org.jetbrains.kotlin.com.intellij.openapi.util.ObjectTree @ 0x707c3e658|            1 |           40 |                48 |        29,720
>                '- myObject2NodeMap gnu.trove.THashMap @ 0x707c3e750                           |            1 |           48 |                48 |        26,800
>                   '- _set java.lang.Object[673] @ 0x70ae496e8                                 |            1 |        2,712 |                48 |         2,712
>                      '- [139] org.jetbrains.kotlin.com.intellij.mock.MockProject @ 0x70836d250|            1 |           48 |                48 |     3,373,448
> ----------------------------------------------------------------------------------------------------------------------------------------------------------------