Fix WebGL shader and texture memory leak on sketch removal#8418
Fix WebGL shader and texture memory leak on sketch removal#8418Sanchit2662 wants to merge 3 commits intoprocessing:mainfrom
Conversation
Add dispose() methods to p5.Shader and p5.Texture classes and register cleanup hook in p5.RendererGL to free GPU resources when remove() is called. Signed-off-by: Sanchit2662 <sanchit2662@gmail.com>
|
Hi @davepagurek, Whenever you have time, I’d appreciate a review. Thanks! |
|
Thanks for taking this on! Some small thoughts:
|
src/webgl/p5.RendererGL.js
Outdated
| this._curShader = undefined; | ||
|
|
||
| // Register cleanup hook to free WebGL resources when sketch is removed | ||
| this._pInst.registerMethod('remove', this._cleanupWebGLResources.bind(this)); |
There was a problem hiding this comment.
The tests are also showing errors that this method is not defined when inside of createGraphics
Signed-off-by: Sanchit2662 <sanchit2662@gmail.com>
|
Hi @davepagurek , |
src/webgl/p5.Texture.js
Outdated
| * @method dispose | ||
| * @private | ||
| */ | ||
| dispose() { |
There was a problem hiding this comment.
Hi! Could we turn this into remove() as well for consistency?
There was a problem hiding this comment.
Hi @davepagurek ,
Fixed — switched this to remove() for consistency. Thanks for pointing it out!
Signed-off-by: Sanchit2662 <sanchit2662@gmail.com>
Summary
This PR fixes a critical WebGL lifecycle issue where shader programs and textures were never explicitly released from GPU memory when a sketch was destroyed. As a result, applications that repeatedly create and remove WebGL sketches (e.g. hot-reload editors, instance-mode apps, long-running installations) experienced unbounded GPU memory growth.
The fix introduces explicit
dispose()methods for WebGL-backed resources and registers a renderer-level cleanup hook that is automatically invoked whensketch.remove()is called.Impact
Before
remove()After
Changes
1.
p5.Shader.dispose()Adds an explicit teardown path for shader GPU resources.
2.
p5.Texture.dispose()Ensures GPU textures are freed when no longer needed.
3. Renderer-level cleanup hook (
p5.RendererGL)Introduces a centralized cleanup method that disposes all WebGL resources owned by the renderer.
Key resources cleaned:
this.textures)The cleanup is automatically invoked on sketch teardown:
PR Checklist
npm run lintpasses