Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak in AudioLevelIndicator #703

Merged
merged 3 commits into from
Jun 21, 2022

Conversation

timmydoza
Copy link
Contributor

@timmydoza timmydoza commented Jun 15, 2022

Contributing to Twilio

All third party contributors acknowledge that any contributions they provide will be made under the same open source license that the open source project is provided under.

  • I acknowledge that all my contributions will be made under the project's license.

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:

var intervalId = setInterval(() => document.querySelector('[data-cy-audio-toggle]').click(), 10);

To stop this, just clear the interval id:

clearInterval(intervalId);

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):

image

Also, you can see the memory leak if you take a heap snapshot and search for classes with "audio" in their name:
image

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

  • Updated CHANGELOG.md if necessary
  • Added unit tests if necessary
  • Updated affected documentation
  • Verified locally with npm test
  • Manually sanity tested running locally
  • Included screenshot as PR comment (if needed)
  • Ready for review

Before merge

  • Got one or more +1s
  • Re-tested if necessary

Copy link
Contributor

@olipyskoty olipyskoty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to work really well!

@timmydoza timmydoza merged commit d4c418c into master Jun 21, 2022
@timmydoza timmydoza deleted the fix-audio-indicator-memory-leak branch June 21, 2022 03:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants