Why does Chrome retain an anonymous MediaRecorder object in memory? Why does Chrome retain an anonymous MediaRecorder object in memory? google-chrome google-chrome

Why does Chrome retain an anonymous MediaRecorder object in memory?


This was confirmed to be an issue with Chromium (and by implication Google Chrome and probably other Chromium derivatives, including Electron):

https://bugs.chromium.org/p/chromium/issues/detail?id=899722

At the time of writing this the issue is flagged as "available", which I presume communicates that it's acknowledged and effectively waiting for someone to step in and fix.


According to the specification:

A MediaStream object is said to be active when it has at least one MediaStreamTrack that has not ended. A MediaStream that does not have any tracks or only has tracks that are ended is inactive.

When you create a new MediaStream, unless you stop all its tracks, it remains in active state.

So, to unset this object, you need to explicitly get rid of its MediaStreamTrack objects, getting a reference to the stream e.g:

var myRecorder;navigator.mediaDevices.getUserMedia({   video: true,  audio: true}).then(function create_media_recorder(stream) {  myRecorder = new MediaRecorder(stream);  console.clear();});var myStream = myRecorder.stream;myStream.getTracks().forEach(function(el) {    el.stop()}

after that, you should see the recording icon going away - The media stream is now inactive.

Check these API's for further reference

https://developer.mozilla.org/en-US/docs/Web/API/MediaStream

https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack


Make sure you have "Preserve log upon navigation" unchecked in your browser's settings, otherwise console.clear() does nothing.

https://developer.mozilla.org/en-US/docs/Web/API/Console/clear

EDIT:

Also, I doubt this is a memory leak since unreachable objects are automatically removed. It's more likely MediaRecorder is saving objects to the global scope.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management#Garbage_collection

"4.3.1 Life-cycle and Media Flow" should clear things up a bit.

https://www.w3.org/TR/mediacapture-streams/#stream-api


matomo