Are WebGL objects garbage collected?

后端 未结 2 2009
情歌与酒
情歌与酒 2020-12-21 09:15

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

相关标签:
2条回答
  • 2020-12-21 09:37

    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.

    0 讨论(0)
  • 2020-12-21 09:46

    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.

    0 讨论(0)
提交回复
热议问题