# PBRCustomMaterial: compiledEffects memory leak

**URL:** <https://forum.babylonjs.com/t/pbrcustommaterial-compiledeffects-memory-leak/63468>\
**Category:** Bugs\
**Tags:** material, pbrcustommaterial\
**Created:** [May 15, 2026, 11:51am UTC](https://forum.babylonjs.com/t/pbrcustommaterial-compiledeffects-memory-leak/63468 "2026-05-15T11:51:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amarth2Estel](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/amarth2estel/32/14588_2.png) [@Amarth2Estel](https://forum.babylonjs.com/u/Amarth2Estel)\
**Post date:** [May 15, 2026, 11:51am UTC](https://forum.babylonjs.com/t/pbrcustommaterial-compiledeffects-memory-leak/63468/1 "2026-05-15T11:51:39Z")

</div>

Hi again,

Still hunting memory leaks in our app, after the fix from my previous post ( [PBRCustomMaterial: ShadersStore memory leak on dispose()](https://forum.babylonjs.com/t/pbrcustommaterial-shadersstore-memory-leak-on-dispose/63452) ) has been merged. They both come from the way the entry key in caches is computed for PBRCustomMaterial.

### The problem

When you mutate a property that changes shader `#defines` on a `PBRCustomMaterial` at runtime (e.g. `transparencyMode`, `albedoTexture`, `backFaceCulling`…), BabylonJS recompiles the shader and adds a new entry to `engine._compiledEffects`. The previous entry (compiled with the old defines) is never removed. It becomes a permanently orphaned `Effect`.

This happens because `PBRCustomMaterial` builds its cache key as `custompbr_<uniqueId>+<defines>`. When defines change, both the old and the new key are unique by construction and will never be reused. `dispose(true, true)` only cleans up whichever key is current at the time of disposal =\> the orphaned one stays forever.

Interestingly, **this also occurs with `PBRMaterial`, but is bounded at most one orphaned entry per define combination** , since the key doesn’t include `uniqueId` and can be reused across instances. With `PBRCustomMaterial`, the `uniqueId` in the key guarantees a new orphan on every cycle, making the leak unbounded.

### Reproduction

Playground: [Babylon.js Playground](https://playground.babylonjs.com/#DWRLUC)

Click the button repeatedly and watch `_compiledEffects` grow by 1 on every click.

`_compiledEffects` is logged:

The pattern that leaks on each cycle:

```javascript
// 1. Material is created and rendered — Effect A compiled
const mat = new BABYLON.PBRCustomMaterial("pbr", scene);
mesh.material = mat;

// 2. Define-changing property mutated — Effect B compiled, Effect A orphaned
mat.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHABLEND;

// 3. Material disposed — Effect B removed, Effect A stays forever
mat.dispose(true, true);

```

Thanks for looking into this!

---

<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:** [May 18, 2026, 10:02am UTC](https://forum.babylonjs.com/t/pbrcustommaterial-compiledeffects-memory-leak/63468/2 "2026-05-18T10:02:24Z")

</div>

Thanks for the detailed repro! We have a fix for this now.

The PR will be created once this larger PR has been merged first: [Tree-shaking - the pure barrel by RaananW · Pull Request #18441 · BabylonJS/Babylon.js · GitHub](https://github.com/BabylonJS/Babylon.js/pull/18441)

---

<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:** [May 19, 2026, 8:12am UTC](https://forum.babylonjs.com/t/pbrcustommaterial-compiledeffects-memory-leak/63468/3 "2026-05-19T08:12:14Z")

</div>

PR has been created:

> <https://github.com/BabylonJS/Babylon.js/pull/18471>
>
> \> 🤖 \*This PR was created by the create-pr skill.\*
> 
> \## Summary
> \- Release the prev…ious \`PBRCustomMaterial\` custom effect when a new ready custom effect replaces it for the same submesh.
> \- Keep the global compiled effect cache behavior intact for other materials and shared effects.
> \- Add unit coverage for the custom material effect replacement cleanup path.
> 
> \## Motivation
> \`PBRCustomMaterial\` uses unique shader names such as \`custompbr\_N\`. When material defines change and a new effect is compiled, the previous effect can become orphaned in \`engine.\_compiledEffects\` because its unique key will not be reused by another material instance.
> 
> Forum report: https://forum.babylonjs.com/t/pbrcustommaterial-compiledeffects-memory-leak/63468
> Related prerequisite PR: https://github.com/BabylonJS/Babylon.js/pull/18441
> 
> \## Validation
> \- \`npx vitest run --project=unit "packages/dev/materials/test/unit/custom/pbrCustomMaterial.test.ts"\`
> \- \`npm run format:check\`

---

<div class="post-metadata">

**Author:** ![Amarth2Estel](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/amarth2estel/32/14588_2.png) [@Amarth2Estel](https://forum.babylonjs.com/u/Amarth2Estel)\
**Post date:** [May 19, 2026, 8:22am UTC](https://forum.babylonjs.com/t/pbrcustommaterial-compiledeffects-memory-leak/63468/4 "2026-05-19T08:22:45Z")

</div>

Thanks a lot for the quick fix !

---

<div class="post-metadata">

**Author:** ![Amarth2Estel](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/amarth2estel/32/14588_2.png) [@Amarth2Estel](https://forum.babylonjs.com/u/Amarth2Estel)\
**Post date:** [May 21, 2026, 2:41pm UTC](https://forum.babylonjs.com/t/pbrcustommaterial-compiledeffects-memory-leak/63468/5 "2026-05-21T14:41:59Z")

</div>

Hello @Evgeni_Popov  
I may be wrong, but since your fix is included in the last BJS release (9.9.0), I expected that previous playground won’t show leaks anymore. It seems there’s a remaining issues about effects leaks as countor still increases.  
Thanks a lot !

---

<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:** [May 21, 2026, 3:57pm UTC](https://forum.babylonjs.com/t/pbrcustommaterial-compiledeffects-memory-leak/63468/6 "2026-05-21T15:57:52Z")

</div>

Thanks for checking 9.9.0. You were right: there was still a remaining cleanup path.

The first fix released the previous custom effect when a new one replaced it, but the effect cache can return the same `PBRCustomMaterial` effect more than once and increment its ref count. On dispose we were only releasing one reference, so the unique `custompbr_*` key could stay in `engine._compiledEffects`.

I opened a follow-up PR that tracks the effects generated for a `PBRCustomMaterial` instance and force-releases those custom effects when the material is disposed: [[Materials] Fix PBRCustomMaterial cached effect cleanup by Popov72 · Pull Request #18493 · BabylonJS/Babylon.js · GitHub](https://github.com/BabylonJS/Babylon.js/pull/18493)

---

<div class="post-metadata">

**Author:** ![Amarth2Estel](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/amarth2estel/32/14588_2.png) [@Amarth2Estel](https://forum.babylonjs.com/u/Amarth2Estel)\
**Post date:** [May 22, 2026, 7:13am UTC](https://forum.babylonjs.com/t/pbrcustommaterial-compiledeffects-memory-leak/63468/7 "2026-05-22T07:13:06Z")

</div>

Thanks again @Evgeni_Popov , it works like a charm with your fix in 9.9.1 ! 👌
