PSA: Garbage collection information on PeerConnections

599 views
Skip to first unread message

Harald Alvestrand

unread,
Dec 5, 2017, 6:48:07 AM12/5/17
to WebRTC-discuss
The spec has this to say about garbage collecting PeerConnections [1]:

"An RTCPeerConnection object must not be garbage collected as long as any event can cause an event handler to be triggered on the object. When the object's [[IsClosed]] internal slot is true, no such event handler can be triggered and it is therefore safe to garbage collect the object.

All RTCDataChannel and MediaStreamTrack objects that are connected to an RTCPeerConnection have a strong reference to the RTCPeerConnection object."

In Chrome, this is implemented as "if the PeerConnection is closed, we'll garbage collect it; if it's not, it's not".

This means that if you fire up a PeerConnection once in a while to check something, and let it run for a while, you risk hitting the limit on number of PeerConnections, which will give you a "sad tab" unless you close the PeerConnection.

To be specific: This is safe:

every few seconds/minutes/hours
   pc = new PeerConnection();
   do stuff
   pc.close()
   pc = null

This will lead to sadness:

every few seconds/minutes/hours
   pc = new PeerConnection()
   do stuff that doesn't close the connection
   pc = null

Enjoy!

Harald



Tomas M

unread,
Aug 29, 2018, 4:22:56 AM8/29/18
to discuss-webrtc
Hello,
this actually does not work, or the garbage collector is broken.

Example code:

var i = 1;
function peer() {
  var peer = new RTCPeerConnection();
  setTimeout(() => {
    peer.close();
    peer=null;
  }, 10);
  console.log(i++);
}

setInterval(peer, 20);

This makes my chrome throw exception after about 500 calls.
Modifying the timeouts to seconds and waiting few hours makes no difference, still problem.
peer variables are not always garbage collected and Chrome reports: Uncaught DOMException: Failed to construct 'RTCPeerConnection': Cannot create so many PeerConnections


Tomas M

Harald Alvestrand

unread,
Aug 29, 2018, 5:45:07 AM8/29/18
to WebRTC-discuss
This doesn't work because the memory allocator never does a garbage collection - it assumes that there is so much memory available, there's no need to garbage collect.




--

---
You received this message because you are subscribed to the Google Groups "discuss-webrtc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss-webrtc+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/discuss-webrtc/4ed505ca-30f8-4568-8de8-020bd9f9e6ad%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages