# How to release ressources for KTX2 textures

**URL:** <https://forum.babylonjs.com/t/how-to-release-ressources-for-ktx2-textures/25176>\
**Category:** Questions\
**Tags:** material\
**Created:** [November 11, 2021, 2:03pm UTC](https://forum.babylonjs.com/t/how-to-release-ressources-for-ktx2-textures/25176 "2021-11-11T14:03:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheHOF](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/thehof/32/19750_2.png) [@TheHOF](https://forum.babylonjs.com/u/TheHOF)\
**Post date:** [November 11, 2021, 2:03pm UTC](https://forum.babylonjs.com/t/how-to-release-ressources-for-ktx2-textures/25176/1 "2021-11-11T14:03:09Z")

</div>

Hi there,

I have an application which has to handle lots of different KTX2 compressed textures.  
When providing all textures in one GLB file (about 20MB) many devices crash while loading the GLB.  
The error messages differ between various browsers but it always seems to related to memory size.  
This is what Chrome reports:  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/c/1/c1e3ed6b770f930fbf25224449d16f9b7fdfe0e8.png)

So I tried to split the textures up into various GLB files and always just load the ones, that are required for the current state of the app.  
Before switching to a different GLB I want to clear the scene, so that the previously loaded textures get disposed and there is enough memory for the new ones.  
However this doesn’t seem to work that way, because after some loading cycles the devices crash again with the same error messages.  
It seems like the memory is still occupied by previously loaded textures.

I made a repro which just adds a GLB to the scene and immediatly disposes the scene afterwards.  
When running that code in a loop I would expect that this could go on forever, since all the ressources of the old scene are disposed, before adding new textures.  
[https://playground.babylonjs.com/#KNCTDK](https://playground.babylonjs.com/#KNCTDK)  
The loop runs about 5 times on my machine in Chrome.  
IOS devices crash already at the 2nd / 3rd time.

Am I missing something, or is there a better way to release the scene ressources?

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [November 11, 2021, 2:40pm UTC](https://forum.babylonjs.com/t/how-to-release-ressources-for-ktx2-textures/25176/2 "2021-11-11T14:40:44Z")

</div>

Do you have the same behaviour with usual textures?

---

<div class="post-metadata">

**Author:** ![TheHOF](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/thehof/32/19750_2.png) [@TheHOF](https://forum.babylonjs.com/u/TheHOF)\
**Post date:** [November 11, 2021, 3:17pm UTC](https://forum.babylonjs.com/t/how-to-release-ressources-for-ktx2-textures/25176/3 "2021-11-11T15:17:00Z")

</div>

@labris It’s definitely also an issue for “standard” textures.

Typically GLBs with standard textures are much larger than GLBs which contain the same textures KTX2 compressed.  
In the beginning of the project we used these standard textures and had real big issues with loading times and even crashes.  
KTX2 solved a lot of these issues. But as the project progresses more and more textures get added and now also KTX2 seems to be on its limit.

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [November 11, 2021, 4:24pm UTC](https://forum.babylonjs.com/t/how-to-release-ressources-for-ktx2-textures/25176/4 "2021-11-11T16:24:12Z")

</div>

You are out of memory - [https://playground.babylonjs.com/#KNCTDK#2](https://playground.babylonjs.com/#KNCTDK#2)  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/a/5/a5952e403b6bbd9290f4ae7dd02217fff794baaf.png)

Here is better disposal (still unstable in Chrome - no problem with Edge) - [https://playground.babylonjs.com/#KNCTDK#4](https://playground.babylonjs.com/#KNCTDK#4)  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/1/b/1b46f9d141d8d8b24dc5713736aced15f4ae89c9.png)

---

<div class="post-metadata">

**Author:** ![syntheticmagus](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/syntheticmagus/32/395_2.png) [@syntheticmagus](https://forum.babylonjs.com/u/syntheticmagus)\
**Post date:** [November 11, 2021, 9:30pm UTC](https://forum.babylonjs.com/t/how-to-release-ressources-for-ktx2-textures/25176/5 "2021-11-11T21:30:46Z")

</div>

Hi TheHOF,

I haven’t been able to repro this crash locally; modern Edge, Chrome Canary, and Firefox all seem to be able to sustain loop iterations up into the thirties on my Windows 11 laptop. (Firefox memory gets _huge_, but never crashes, presumably just turning up GC when memory pressure gets high.) Can you provide some more information about the repros you’ve experienced? In particular, what Babylon versions, browsers, and platforms have you seen this repro on? Thanks!

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [November 11, 2021, 9:40pm UTC](https://forum.babylonjs.com/t/how-to-release-ressources-for-ktx2-textures/25176/6 "2021-11-11T21:40:07Z")

</div>

Running your PG I see no leak:  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/6/c/6c487a3ff5328da901846a0dee3c8db0139f3d02.png)

Last snapshot was made 5 minutes after the first one

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [November 11, 2021, 11:48pm UTC](https://forum.babylonjs.com/t/how-to-release-ressources-for-ktx2-textures/25176/7 "2021-11-11T23:48:57Z")

</div>

On my machine with 64GB of RAM, it repros, but only after 134 times. 😮

EDIT1: Well, it doesn’t repro the crash. The wasm module fails with an exception.  
EDIT2: This is with Edge.

EDIT3: The browser task manager definitely indicates some kind of memory leaking.

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/0/d/0d27923fb0a326f2029c9b75a2dc3be0d5648da0.png)  
8GB and rising 😮

EDIT4: Second try, similar result. After 137 tries, it dies with same error as before.

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/d/3/d3afafc57b7eab24009614eefb43d12da09234c1.png)

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [November 12, 2021, 12:00am UTC](https://forum.babylonjs.com/t/how-to-release-ressources-for-ktx2-textures/25176/8 "2021-11-12T00:00:40Z")

</div>

With this PG - [https://playground.babylonjs.com/#KNCTDK#4](https://playground.babylonjs.com/#KNCTDK#4) - no problem with Chrome on Mac (32 Gb RAM) and Edge on Windows (8 Gb RAM), Chrome on Windows crashes after the 5th render .

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [November 12, 2021, 12:12am UTC](https://forum.babylonjs.com/t/how-to-release-ressources-for-ktx2-textures/25176/9 "2021-11-12T00:12:52Z")

</div>

@labris Your PG dies for me exactly the same way.

We’re investigating still, but it seems like there is a leak in WASM memory somewhere. If we reduce the number of workers to 1, then it dies at 3.5GB memory instead of 10GB, which seems to imply that each worker is running out of memory.

---

<div class="post-metadata">

**Author:** ![syntheticmagus](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/syntheticmagus/32/395_2.png) [@syntheticmagus](https://forum.babylonjs.com/u/syntheticmagus)\
**Post date:** [November 12, 2021, 4:22am UTC](https://forum.babylonjs.com/t/how-to-release-ressources-for-ktx2-textures/25176/10 "2021-11-12T04:22:29Z")

</div>

After further investigation, there definitely appears to be a leak, but its over in the Web workers that do the WASM work, so it looks like it doesn’t show up in the `performance.memory` traces from the main thread. Tracking that down and fixing it will be one workstream, but we’re also considering another feature that would allow you to tell the `Engine` type to let go of its loader resources altogether, which might be helpful for memory consumption in the long term and can certainly be used as a workaround for the leak in the immediate term (by periodically releasing and recreating the KTX2 loader workers, which gets rid of the leaked memory).

[ThinEngine.MinimizeMemoryFootprint by syntheticmagus · Pull Request #11442 · BabylonJS/Babylon.js (github.com)](https://github.com/BabylonJS/Babylon.js/pull/11442)

Using this as a workaround, I was able to confirm that I could run for 25 minutes and over 300 iterations without the overall memory footprint continuing to grow. A little off the wall, but just wanted to keep you updated since this is the thread that got us looking at this. 🙂

---

<div class="post-metadata">

**Author:** ![TheHOF](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/thehof/32/19750_2.png) [@TheHOF](https://forum.babylonjs.com/u/TheHOF)\
**Post date:** [November 12, 2021, 6:03am UTC](https://forum.babylonjs.com/t/how-to-release-ressources-for-ktx2-textures/25176/11 "2021-11-12T06:03:54Z")

</div>

Thank you all for your amazing support, I’m very glad if I could help finding memory leaks in here.

@syntheticmagus Is there already a test version of your workaround which I could test in my project?

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [November 12, 2021, 12:13pm UTC](https://forum.babylonjs.com/t/how-to-release-ressources-for-ktx2-textures/25176/12 "2021-11-12T12:13:21Z")

</div>

> [@TheHOF](#):
>
> @labris It’s definitely also an issue for “standard” textures.

I changed the model to the GLTF with “standard” textures - [https://playground.babylonjs.com/#KNCTDK#11](https://playground.babylonjs.com/#KNCTDK#11) - and it goes endlessly at my poor old laptop 🙂  
What I would suggest as possible (or temporary) solutions:

1. Your model has a lot of 4K textures. Are they really needed for visuals or you may downscale them to 2048 or even 1024?
2. You may try to load with GLB just only geometry and materials and then load needed textures async.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [November 12, 2021, 3:32pm UTC](https://forum.babylonjs.com/t/how-to-release-ressources-for-ktx2-textures/25176/13 "2021-11-12T15:32:26Z")

</div>

To have the ktx2 decoder do its work in the main js thread, set `BABYLON.KhronosTextureContainer2.DefaultNumWorkers = 0;`: that will disable the worker thread pool.

Doing so, we can indeed see that the snapshots are growing:  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/d/7/d7ea093f69e33f637f16edeb4e2ee02ae0077ab3.png)

The culprit is the msc basis transcoder module:

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/c/1/c184ddad809ef4869fa78c2485097d7bf15b6022.png)

It seems the heap used by the component is growing indefinitely…

We already had some problems in the past with this module (see ["Cannot perform Construct on a detached ArrayBuffer" error when using msc\_basis\_transcoder - Giters](https://www.giters.com/KhronosGroup/KTX-Software/issues/371)).

Are you able to encode your textures in UASTC instead of ETC1S? We use some special wasm modules to handle UASTC-\>BC7/ASTC/RGBA so it could be a workaround (also, they are way faster to transcode than msc\_basis\_transcoder).

---

<div class="post-metadata">

**Author:** ![syntheticmagus](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/syntheticmagus/32/395_2.png) [@syntheticmagus](https://forum.babylonjs.com/u/syntheticmagus)\
**Post date:** [November 12, 2021, 6:12pm UTC](https://forum.babylonjs.com/t/how-to-release-ressources-for-ktx2-textures/25176/14 "2021-11-12T18:12:51Z")

</div>

> [@TheHOF](#):
>
> Is there already a test version of your workaround which I could test in my project?

We may not end up adopting it, or the form may change; but if you really want to try my current workaround you can pull the branch from my PR and use the `Engine`’s new `MinimizeMemoryFootprint()` method periodically to cause the `Engine` to discard static resources, including the KTX2 worker pool. However, there’s a lot of other great advice that’s been added to this thread, too, so I’d probably only try that as a last resort. 🙂

---

<div class="post-metadata">

**Author:** ![TheHOF](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/thehof/32/19750_2.png) [@TheHOF](https://forum.babylonjs.com/u/TheHOF)\
**Post date:** [November 15, 2021, 7:54am UTC](https://forum.babylonjs.com/t/how-to-release-ressources-for-ktx2-textures/25176/15 "2021-11-15T07:54:11Z")

</div>

Thx again for all your great tips.  
At first we will try to adjust the compression mode (UASTC), since we already have another issue that could probably be related to the ETC compression:

> [@OpacityTexture not working on IOS 15 devices](https://forum.babylonjs.com/t/opacitytexture-not-working-on-ios-15-devices/25102):
>
> Hi everyone, I am having troubles with the opacity map on IOS devices with the latest IOS version (15 at time of writing). The opacity just doesn’t seem to get applied, whereas the very same code works on IOS devices pre version 15 and windows PCs or android devices. Here is my example: [Babylon.js Playground](https://playground.babylonjs.com/#E2FYTU) Sprial correctly shown on IOS version \< 15: [image] Torus shown on IOS version 15: [image] Sry for the long loading times, the example is from a larger application and I wasn’t able …

I will keep you informed.

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [December 13, 2021, 12:26pm UTC](https://forum.babylonjs.com/t/how-to-release-ressources-for-ktx2-textures/25176/16 "2021-12-13T12:26:58Z")

</div>

Hi there @TheHOF ! Just checking in to see how’s your issue, has it been solved? 🙂

---

<div class="post-metadata">

**Author:** ![TheHOF](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/thehof/32/19750_2.png) [@TheHOF](https://forum.babylonjs.com/u/TheHOF)\
**Post date:** [December 13, 2021, 12:51pm UTC](https://forum.babylonjs.com/t/how-to-release-ressources-for-ktx2-textures/25176/17 "2021-12-13T12:51:49Z")

</div>

Hi @carolhmj,  
we managed to solve our application by reloading the page when materials from a different GLB are required, since this was the only way to release the ressources.  
It works fine for us and we know how to work this around in the future, but I guess the original problem is still there.

---

<div class="post-metadata">

**Author:** ![CbnMike](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cbnmike/32/17247_2.png) [@CbnMike](https://forum.babylonjs.com/u/CbnMike)\
**Post date:** [December 13, 2021, 1:12pm UTC](https://forum.babylonjs.com/t/how-to-release-ressources-for-ktx2-textures/25176/18 "2021-12-13T13:12:52Z")

</div>

I’d like to add something to Markus’s comment:

The mentioned solution using a page reload is actually a rather horrible workaround and we’d really appreciate if the underlying memory issues would be tackled in Babaylon JS at some point to allow us better control of the whole memory management by e.g. releasing resources which are not used anymore to free up memory etc.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [December 13, 2021, 10:41pm UTC](https://forum.babylonjs.com/t/how-to-release-ressources-for-ktx2-textures/25176/19 "2021-12-13T22:41:37Z")

</div>

@bghgary do you know if there were any progress made on the ktx loader itself ?

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [December 13, 2021, 11:26pm UTC](https://forum.babylonjs.com/t/how-to-release-ressources-for-ktx2-textures/25176/20 "2021-12-13T23:26:47Z")

</div>

There has been some movement, but I’m not sure it will address the memory issues. Notably, the msc\_basis\_transcoder WASMs are now generated from the basis github instead of KTX as noted here:

[Releases · KhronosGroup/KTX-Software (github.com)](https://github.com/KhronosGroup/KTX-Software/releases)

> _NOTE:_ that msc\_basis\_transcoder is deprecated and will be replaced by the transcoder wrapper from the [Basis Universal](https://github.com/BinomialLLC/basis_universal) repository.

We can try updating to see if there is improvement.

[Next page](https://forum.babylonjs.com/t/how-to-release-ressources-for-ktx2-textures/25176.md?page=2)
