godot-kotlin-jvm icon indicating copy to clipboard operation
godot-kotlin-jvm copied to clipboard

Scenes with lots of nodes close extremely slowly due to excessive JVM Garbage Collector invocations.

Open ImplementsLegend opened this issue 1 year ago • 2 comments

Simple scenario:

class Container:Node(){
    init{
        repeat(1000000){
            addChild(Node())
        }
    }
}

Godot runs absolutely fine, but when you try to close it, it takes forever.

Problem is that MemoryManager.cleanup() method invokes garbage collector too much. (at least once for every 256 objects)

internal object MemoryManager {
    /*omitted*/
    fun cleanUp() {
        /*omitted*/
        var begin = Instant.now()
        while (ObjectDB.any { it != null } || nativeCoreTypeMap.isNotEmpty()) { //loops until all objects are cleaned

            forceJvmGc() //runs JVM Garbage Collector
            if (manageMemory()) { //cleans up to 256 objects
                begin = Instant.now()
            }
            /*omitted*/
    }
}

So, to clean up 1000000 nodes, forceJvmGc() will be invoked approximately 4000 times and that takes a long time.

ImplementsLegend avatar Jun 09 '24 18:06 ImplementsLegend

It's a known issue that this part of the code is slow. We plan to replace it following this existing proposal: https://github.com/utopia-rise/godot-kotlin-jvm/issues/508

The direct consequence will be that we won't need to wait for the GC to run when closing Godot anymore.

CedNaru avatar Jun 13 '24 14:06 CedNaru

I am having as well a slow shutdown when closing my game. I am creating a 3d map with a lot of tile-nodes and I am guessing it is this issue.

kostaskougios avatar Jun 23 '24 00:06 kostaskougios

Should be solved by #661

CedNaru avatar Aug 31 '24 09:08 CedNaru