如何清除关闭的RTCPeerConnection>>[与替代方法一起使用] [英] How to clear closed RTCPeerConnection >> [with WORKAROUND]

查看:56
本文介绍了如何清除关闭的RTCPeerConnection>>[与替代方法一起使用]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是古老的著名Chromium错误: https://bugs.chromium.org/p/chromium/issues/detail?id = 825576

This is the old famous Chromium bug: https://bugs.chromium.org/p/chromium/issues/detail?id=825576

错误是:无法构建'RTCPeerConnection':无法创建那么多PeerConnections

现在,由于Edge基于Chromium,不仅Chrome受此漏洞影响,甚至使情况变得更糟.

Now because Edge is based on Chromium, not only Chrome is affected by this bug making things even worst.

我们需要找到一种方法来强制垃圾收集器循环.

We need to find a way to force Garbage Collector cycle.

我发布了当前的解决方法,但很高兴找到更好的解决方法(如果有的话...?)

I posted my current workaround but i'd be glad to find a better workaround, if any...?

推荐答案

经过一段时间的摸索后,我发现强制/调用垃圾收集器的最佳解决方法是先创建然后撤销一些数据缓冲区.

After some times trying to figure it out, the best workaround i found to force/invoke Garbage Collector is to create then revoke some data buffers.

要使用对Chrome/Edge的最简单的修复方法:

Simplest fix on Chrome/Edge was to use:

URL.revokeObjectURL(URL.createObjectURL(new Blob([new ArrayBuffer(5e+7)]))) // 50Mo buffer

,那么这会在Firefox上导致内存泄漏.在Firefox上,似乎没有绑定到DOM元素就无法撤销ObjectURL.在规范中找不到任何内容.

BUT then, this would introduce memory leaks on Firefox. On Firefox, it seems like ObjectURL cannot be revoked without being bound to DOM element. Cannot find anything about it in the spec.

因此,跨浏览器解决方案(Chrome/Edge/Firefox,未测试其他浏览器)将是:

So cross browser solution (Chrome/Edge/Firefox, other browsers not tested), would be:

queueMicrotask(() => { // || >> requestIdleCallback
  let img = document.createElement("img");
  img.src = window.URL.createObjectURL(new Blob([new ArrayBuffer(5e+7)])); // 50Mo
  img.onerror = function() {
    window.URL.revokeObjectURL(this.src);
    img = null
  }
})

以下是修复WebRTC错误的示例工作代码:

Here is a sample working code fixing WebRTC bug:

var i = 1;

function peer() {
  var peer = new RTCPeerConnection();
  setTimeout(() => {
    peer.close();
    peer = null;
  }, 10);
  console.log(i++);
  if (!(i % 20)) {
    // try to invoke GC on each 20ish iteration
    queueMicrotask(() => { 
      let img = document.createElement("img");
      img.src = window.URL.createObjectURL(new Blob([new ArrayBuffer(5e+7)])); // 50Mo or less or more depending as you wish to force/invoke GC cycle run
      img.onerror = function() {
        window.URL.revokeObjectURL(this.src);
        img = null
      }
    })
  }
}

setInterval(peer, 20);

这篇关于如何清除关闭的RTCPeerConnection>>[与替代方法一起使用]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆