Fix memory leak in AudioLevelIndicator #703
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Contributing to Twilio
Pull Request Details
JIRA link(s):
Description
This PR fixes an issue where the AudioLevelIndicator can cause a memory leak after it is mounted/unmounted many times.
To see the issue, join a room, and run this code in the console which will repeatedly click the "Mute Audio" button:
To stop this, just clear the interval id:
As the local audio track is repeatedly muted and unmuted, it will cause the AudioLevelIndicator to turn on and off. While this happens, you should see the memory footprint for the tab start to grow drastically (use Chrome's task manager to see the memory usage):
Also, you can see the memory leak if you take a heap snapshot and search for classes with "audio" in their name:
You'll see that there's a huge number of
MediaStreamsAudioSourceNode
classes.This the fix in the PR, you will no longer see 1) the memory footprint grow, or 2) the large amount of
MediaStreamsAudioSourceNode
classes in the heap.This memory leak was caused by the fact that all AudioLevelIndicators used to share a single AudioContext. Because of this, all MediaStreamsAudioSourceNodes that were created could not be garbage collected because the single AudioContext instance was never closed.
This this PR, each AudioLevelIndicator now uses their own AudioContext instance, which is then closed when it is no longer needed. As a result, all resources used by the AudioLevelIndicator are now appropriately garbage collected.
Burndown
Before review
npm test
Before merge