react-three-fiber icon indicating copy to clipboard operation
react-three-fiber copied to clipboard

Leaking WebGLRenderer and more when unmounting

Open Jashpatel1 opened this issue 2 years ago • 11 comments

I am using r3f for a 3d viewer web-app. When I try to navigate outside of the 3D-containing component and then navigate back into it (change of props) (in effect rerendering the <Canvas>), It creates a new WebGL2 canvas everytime whose context is not being lost. After hitting a certain number of canvases, safari forces a reload and crashes the app. This issue is only reproduced on Safari and Chrome on iOS. On Android Chrome or web chrome it works fine. I have tried manually forcing the canvas to dispose: gl.dispose() gl.forceContextLoss() gl = undefined I have also disposed of all objects using dispose from react-three-fiber: dispose(model.scene) Screenshot 2023-11-10 at 5 19 21 PM

I have seen references to the same issue here: https://github.com/pmndrs/react-three-fiber/issues/514

Jashpatel1 avatar Nov 10 '23 11:11 Jashpatel1

@Jashpatel1 Could you share your code? It's difficult to provide any help with this issue without understanding the underlying context

emnul avatar Nov 20 '23 01:11 emnul

I'm encountering a persistent problem involving a potential memory leak within a component of react-three-fiber, specifically with the WebGLRenderer and associated elements. Despite following the documentation diligently, I've noticed that WebGL objects continue to be retained, leading to a gradual increase in memory usage when unmounting and remounting the component.

I've managed to reproduce this issue in a CodeSandbox example, which you can access here: https://codesandbox.io/p/devbox/r3f-memory-leak-forked-x8vmfs. In this sandbox, you'll observe the WebGLRenderer leak. Furthermore, our application, which loads glTF scenes, is also prone to similar leaks with the renderer.

Any insights or guidance on addressing this potential issue with react-three-fiber would be immensely appreciated.

lalit-kumar-1994 avatar Mar 07 '24 11:03 lalit-kumar-1994

@emnul here is the Code-Sandbox link - https://codesandbox.io/p/devbox/r3f-memory-leak-forked-x8vmfs

Also, here is the attached video of the memory leak which we are facing : https://drive.google.com/file/d/1fUc4Fpq4lztguSoM-h929dcJ0N0OLWnH/view?usp=sharing

Please help us with this memory leak issue

prabhasjoshiavataar avatar Mar 08 '24 07:03 prabhasjoshiavataar