WebGLTextures是否收集垃圾? [英] Are WebGLTextures garbage collected?
问题描述
WebGLTexture? createTexture();
void deleteTexture(WebGLTexture?texture);
尽管在GLES中您使用数字(地址?)来引用纹理,但在WebGL中,一个 WebGLTexture
对象。
这是否意味着如果这样的对象超出范围并且被垃圾收集,关联的纹理也将从GPU中删除?如果没有,为什么不呢?
(这是依赖于实现的 - 至少该规范没有提到它,我认为 - 所以我对主要浏览器感兴趣。)
是且不是。
但是无论何时浏览器决定收集垃圾收集都会发生垃圾收集。从大多数浏览器JavaScript引擎的POV中, WebGLTexture
对象是一个小对象,它只包含一个 int
,所以它没有简单的方法来知道收集它的任何特殊压力。换句话说,当OpenGL运行内存不足时,JavaScript垃圾回收器与OpenGL没有任何联系,因此无法知道它需要按顺序释放这些微小的 WebGLTexture
对象释放纹理内存。它只是查看CPU内存。
这实际上是一个众所周知的垃圾收集问题。记忆力很棒。这对于其他资源来说并不是那么好。
所以,
当然,如果刷新页面,浏览器将释放它们全部或者在同一个标签中访问新页面,但不能指望浏览器以任何有用的方式垃圾收集 WebGLTextures
(或任何其他WebGL资源)。
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.)
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.
这篇关于WebGLTextures是否收集垃圾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!