-
Notifications
You must be signed in to change notification settings - Fork 15
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
Garbage collector problem #9
Comments
You arguably should not be counting on garbage collection for WebGL. As is says in the MDN article for WeakRef Avoid where possibleCorrect use of WeakRef takes careful thought, and it's best avoided if possible. It's also important to avoid relying on any specific behaviors not guaranteed by the specification. When, how, and whether garbage collection occurs is down to the implementation of any given JavaScript engine. Any behavior you observe in one engine may be different in another engine, in another version of the same engine, or even in a slightly different situation with the same version of the same engine. Garbage collection is a hard problem that JavaScript engine implementers are constantly refining and improving their solutions to. Here are some specific points that the authors of the WeakRef proposal included in its explainer document:
On top of all those reasons it's even worse with WebGL. AFAIK no JavaScript engine has any idea of the memory used by WebGL resources. In other words, Assume you have 150meg free of VRAM. If you allocate a 100meg texture, from JavaScript's POV all you've done is allocate a few bytes to track the the WebGL resource. The JavaScript engine has no idea of the 100meg if you've allocated in VRAM. If you then lose the reference and immediately allocate another 100meg texture the JavaScript engine has no idea it should stop the world and delete your unreferenced 100meg texture so that there's room to allocate a new one. Instead you'll likely just get an OUT_OF_MEMORY error from WebGL or you'll lose the WebGL context. Conversely if you I really don't want to encourage bad practices via this library. This library requires you to delete your WebGL objects which is the right thing to do and it will report resource counts encouraging you to manually delete the objects. |
Just as a test I wrote some code that allocated a 4Meg WebGL texture and watched for it to be garbage collected using As another test I tried creating a Canvas and waiting to see when it was collected. On Safari it was collected in ~4 seconds. On Firefox ~9. On Chrome it was ~32 seconds. In this case, every second I created a new canvas and inserted in into the body of the document. When I removed the creation of another canvas every second it was ~4 seconds on Safari, ~9 on Firefox, stop waiting after 8 minutes in Chrome Anyway, this is just all evidence of the warnings above. |
Also, just for fun, I tried implementing it. I couldn't get Chrome nor Firefox to garbage collect WebGL objects so there's no way to test 😅 But, also, unless there's some other way to implement it I don't know, I'm required to keep an array of all
Maybe that's okay. It's just slow for a large project to go over that array. I guess if you're not calling One more issue is that in order to work, webgl-memory would have to start tracking all attachments (see README on the fact that it doesn't). The reason is the browser can GC a WebGLBuffer, WebGLTexture, or WebGLRenderbuffer object but WebGL can still reference that object if it's attached to some other object. For example a buffer referenced in a vertex array or a texture referenced in a framebuffer Here's an example (only works in Safari) The That's not to say it shouldn't be done. It's just more work than I'm willing to do at the moment. |
Garbage collector cannot delete textures from gpu memory when using this extension!
Is this a bug or is it normal?
If this is normal, can't the problem be fixed using WeakRef?
The text was updated successfully, but these errors were encountered: