-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Fix WebGL shader and texture memory leak on sketch removal #8418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Fix WebGL shader and texture memory leak on sketch removal #8418
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi! Could we turn this into remove() as well for consistency?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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