首页 > 解决方案 > 如何在当前版本清理 textureCache A-frame?

问题描述

我正在开发一个多摄像头流媒体应用程序,我注意到切换摄像头后内存使用量不断增加。我在 hls.js 方面进行清理。但我没有看到任何方法可以在框架中做到这一点。我正在使用 1.2.0

我只发现旧帖子推荐
document.querySelector('a-scene').systems.material.textureCache并运行.dispose()

这看起来像是在 0.3.0 版本上工作的,但从那以后就没有了。

有没有办法清理纹理或者这现在是自动发生的?

标签: javascriptthree.jsaframehls.js

解决方案


AfaiktextureCache是一个承诺加载纹理(图像视频)的对象。

有一个clearTextureCache功能,但它清除对象,而不处理加载的纹理。

我会尝试遍历textureCache,抓取THREE.Texture对象并调用.dispose()它们。然后你可以做clearTextureCache()清理它。在下面的示例中 - 任何单击 io 窗口都会在控制台中打印缓存的纹理:

<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
<script>
  AFRAME.registerComponent("foo", {
    init: function() {
      window.addEventListener("click", e => {
        const textureCache = this.el.systems.material.textureCache;
        console.log("Textures in the cache:")
        for (let key in textureCache) {
          textureCache[key].then(val => console.log(val))
        }
      })
    }
  })
</script>
<a-scene foo>
  <a-image position="-1 1.6 -2" src="https://i.imgur.com/wjobVTN.jpeg"></a-image>
  <a-image position="1 1.6 -2" src="https://i.imgur.com/AD3MbBi.jpg"></a-image>
  <a-sky color="#ECECEC"></a-sky>
</a-scene>


推荐阅读