Workers of KTX2 Loader are never closed

Hi,

I’m trying to implement KTX2 textures in our application but I have some problems with the memory footprint. It seems that the KTX Loader creates new workers (maximum 4 according to the BJS source code) to load the textures but these workers are never closed.

This leads to a significant increase in memory usage.
Tested on versions 4.2.1 and 5.0.0

Here are some playgrounds to test the loading of KTX2 (with Chrome TaskManager for eg):
KTX2 vs JPG

The 4 workers are only created with KTX2 texture (not with JPG) and never closed:

I also tested to override the KTXTextureLoader.loadData function to call the dispose function. This change terminates the workers well and frees the associated memory but it prevents to load a ktx2 texture again. I guess this is not intended to.
When I close the workers, the memory used goes from 500 mB to 170 mB (for one of our objects) and the application continues to work properly.

Do you have any idea if this is a BabylonJS bug or just a bad implementation on my part?

Thanks in advance,

2 Likes

@bghgary would be great if you could have a look ?

1 Like

Still investigating, but it does appear we don’t dispose the KhronosTextureContainer2 object.

1 Like

I will try to introduce a feature our worker pool class to solve this. It may take a bit of time.

3 Likes

Thank you for your responsiveness,

I have set up a Quick Fix for our project which seems to solve the problem.
I rewrote the KTXTextureLoader.loadData function to keep the reference to the new KhronosTextureContainer2 created.

Here is the modified line in the function :

TexturesLoader.KTX2Container = new KhronosTextureContainer2(texture.getEngine());

TexturesLoader.KTX2Container is a public static KhronosTextureContainer2 to keep the reference.
When my textures are all loaded, I call a dispose function :

    disposeKTX2Container() {
        if (!TexturesLoader.KTX2Container) return;
        TexturesLoader.KTX2Container.dispose();
        TexturesLoader.KTX2Container = null;
        (KhronosTextureContainer2 as any)._Initialized = false;
    }

I’m still trying to figure out if this would create any bugs or memory leaks.

4 Likes

It should be fine as long as you don’t have two KTX2 containers at the same time. I have a local change for WorkerPools that will clean up automatically.

1 Like

PR: Add and use AutoReleaseWorkerPool for auto cleanup by bghgary · Pull Request #12096 · BabylonJS/Babylon.js · GitHub

2 Likes