5

In WebGL, textures are created and destroyed using:

WebGLTexture? createTexture();
void deleteTexture(WebGLTexture? texture);

Whereas in GLES you use a number (address?) to refer to a texture, in WebGL there is a WebGLTexture object.

Does this mean that if such an object goes out of scope and is garbage collected, its associated texture will also be deleted from the GPU? If not, why not?

(This is implementation dependent - at least the spec doesn't mention it I think - so I'm interested in the major browsers.)

Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301
  • PS: I know this question was asked before the other but S.O. is a collection of answers and so linking to the more generic question seemed appropriate – gman Oct 22 '19 at 13:51

1 Answers1

5

Yes and no.

Yes it is garbage collected. But garbage collection happens whenever the browser decides to collect it. From the POV of most browsers JavaScript engines the WebGLTexture 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 OpenGL runs out of memory the JavaScript garbage collector, which has no connection to OpenGL, has no way of knowing that it needs to free these tiny WebGLTexture 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, WebGLTextures are garbage collected and yes the texture 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 WebGLTextures (or any other WebGL resource) in any useful way.

gman
  • 100,619
  • 31
  • 269
  • 393
  • Doesn't OpenGL running low on memory trigger the GC, and doesn't that GC clean up everything it can, tiny though it may be? Also, assuming the `WebGLTexture` is still connected in the garbage object graph to the DOM canvas which is the source of the texture, and thus pretty heavy, would that be enough to have it be collected? – Bart van Heukelom Jul 07 '15 at 12:05
  • Oh, and it's been a while, but I've worked on a project embedding V8. I remember that you can manually assign a specific 'retained memory size' to objects, so that even if they are small in Javascript contents, the GC knows they retain a lot. Is that not used? – Bart van Heukelom Jul 07 '15 at 12:08
  • Won't work. GPU memory is not the same as CPU memory. Then GPU memory is full there's no pressure on the GC itself. Even if there was it would require it to know which objects represent CPU memory and which objects represent GPU memory. I don't think any browser as implemented anything like that. On top of that GPU memory is shared which means things out of control of the GC's management can affect how much is available. – gman Jul 07 '15 at 14:47