6

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 Buffers or Textures are associated with a memory block on the GPU as allocated by gl.bufferData() or gl.textureImage2D().

I'm wondering: if I give up my last reference to a WebGLTexture or WebGLBuffer object, does it get garbage collected with its GPU memory block freed by the JavaScript VM automatically?

Danny Raufeisen
  • 953
  • 8
  • 21
  • I believe no. plus, `gl.bufferData()` and `gl.textureImage2D()` doesn't really return any reference object iirc. – apple apple Oct 22 '19 at 08:34
  • @appleapple I know. The reference is created with `gl.createBuffer()` and later this reference is assigned a memory block with `gl.bufferData()`. Let's also consider I unbound the buffer targets `gl.ARRAY_BUFFER` and `gl.ELEMENT_ARRAY_BUFFER` – Danny Raufeisen Oct 22 '19 at 08:56
  • well, it seems I was wrong. it would automatically delete. – apple apple Oct 22 '19 at 09:03

2 Answers2

4

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.

apple apple
  • 10,292
  • 2
  • 16
  • 36
  • Thank you! :) So that means, I wouldn't necessarily open a memory leak if I would never call gl.deleteBuffer() manually. – Danny Raufeisen Oct 22 '19 at 09:20
  • 1
    @DannyRaufeisen I'd not rely on the GC to collect my object. It may still cause problems. After all, as long as you close the tab, all resources should be released. – apple apple Oct 22 '19 at 09:26
3

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.

gman
  • 100,619
  • 31
  • 269
  • 393