Skip to content Skip to sidebar Skip to footer

Are Webgl Objects Garbage Collected?

In JavaScript memory that I allocated (e.g. an ArrayBuffer) gets freed up when I don't have any reference to it anymore by the GC as I understood that right? WebGL objects like Buf

Solution 1:

Yes


From spec

Note that underlying GL object will be automatically marked for deletion when the JS object is destroyed


But you should notice that the object would probably not destroyed at the time you give up the last reference, so it's still a good practice to call deleteBuffer.

Solution 2:

Copied from this answer: Are WebGLTextures garbage collected?

with the word WebGLTexture changed to WebGLObject


Yes and no.

Yes they are garbage collected. But garbage collection happens whenever the browser decides to collect them. From the POV of most browser JavaScript engines the WebGLObject object is a tiny object that just contains an int so it has no easy way to know of any special pressure to collect it. In other words when the GPU runs out of memory the JavaScript garbage collector, which has no connection to the GPU, has no way of knowing that it needs to free these tiny WebGLObject objects in order to free up texture memory. It's only looking at CPU memory.

This is actually a well known problem of garbage collection. It's great for memory. It's not so great for other resources.

So, yes, WebGLObject objects are garbage collected and yes the texture/buffer/renderbuffer/program/shader will be freed but practically speaking you need to delete them yourself if you don't want to run out of memory.

Of course the browser will free them all if you refresh the page or visit a new page in the same tab but you can't count on the browser to garbage collect WebGLObject objects (textures/buffers/renderbuffers/programs/shaders) in any useful way.

Post a Comment for "Are Webgl Objects Garbage Collected?"