内存泄漏:JavaScript-使用可转移对象将数组缓冲区传递给Web Worker

发布于 2025-02-04 02:33:14 字数 526 浏览 2 评论 0原文

因此,我将视频缓冲区(float32array缓冲区)传递到工作线程作为可转移对象,它发生在视频的每个帧(含义,连续的视频帧都传递给工作人员)。我进行了内存分析,并且工作人员线程保留了对象,似乎垃圾收集器没有启动。如何处理这种情况?我认为没有人可以在JavaScript中明确调用GC。

这是我如何将视频缓冲区传递给工作线程的代码片段:

this.worker.postMessage({
          command: 'SetVideoBuffer',
          data: {
            videoFrame: videoFrame
          }
        },
          [videoFrame.buffer]);

另外,我尝试将缓冲区设置为“ null”,这无济于事。

知道如何解决这个问题,这是镀铬的吗?请帮忙。

谢谢你!

So, I am passing video buffer(Float32Array buffer) to worker thread as a transferable object, it happens for each frame of the video(meaning, continuously video frames are being passed to worker). I did memory profiling, and worker thread is retaining the object, it seems garbage collector doesn't kick in. How to handle this case? I don't think one can invoke GC explicitly in JavaScript.

Here is the code snippet how I am passing video buffer to worker thread:

this.worker.postMessage({
          command: 'SetVideoBuffer',
          data: {
            videoFrame: videoFrame
          }
        },
          [videoFrame.buffer]);

Also, I tried setting the buffer at worker side to 'null', it didn't help.

Any idea how to resolve this, is this chrome thing? Please help.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东风软 2025-02-11 02:33:14

因此,我得到了这个问题的解决方法,看起来如果有人将视频缓冲区发送回到创建的主线程中,GC确实会启动并清除堆。

通过使用相同的零拷贝(可转移对象)方式,我将缓冲区转移回主线程,内存泄漏问题得到了解决。

So, I got the workaround of this problem, looks like if one sends back the video buffer back to main thread where it was created, GC does kick in and clear the heap.

By using the same, zero-copy(transferable objects) way, I transferred the buffer back to the main thread, memory leak issue got resolved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文