-
Notifications
You must be signed in to change notification settings - Fork 100
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
Huge memory leaks produced by publishing/unpublishing camera tracks #420
Comments
Thanks for the detailed report, will investigate this. |
Hello I do see huge memory leaks, I did a quick investigation but I can't find where at the moment. |
Hello @hiroshihorie! The memory leaks are caused by the LKRTCRtpTransceiver media channels that are not closed until the LKRTCPeerConnection is also closed. For several publish -> unpublish operations the transceivers created for every published track don’t get removed from LKRTCPeerConnection by using You can check this using my demo application (the one that I left in the first comment), switching the video on -> off multiple times, and then watching the number of the sender when you disconnect from the Room. You will see that the sender's count is equal with count of the publishments that you have done. I tried to solve the problem by stopping the transceiver when the track is removed removal. extension Transport {
...
func remove(track sender: LKRTCRtpSender) throws {
guard let transceiver = _pc.transceivers.first(where: { $0.sender == sender }) else {
throw LiveKitError(.webRTC, message: "Invalid track")
}
transceiver.sender.track = nil
transceiver.stopInternal()
guard _pc.removeTrack(sender) else {
throw LiveKitError(.webRTC, message: "Failed to remove track")
}
}
...
} The transceiver is removed from the peer connection and there are no more memory leaks. From the official WebRTC implementation and from what I read from the internet, transceiver persistence over the peer connection is something intentional on Unified Plan. See:
I tested and the same problem happens also on the Android client. A fast workaround that can be applied right now would be to reuse the inactive transceivers. |
Describe the bug
I encountered huge memory leaks (around 50MB) when I attempted to publish -> unpublish camera tracks several times.
SDK Version
2.0.11
iOS Version
Tested on iOS 17 and iOS 15
Xcode Version
Version 15.4 (15F31d)
Swift version: Apple Swift version 5.10
Steps to Reproduce
Demo Application
I created a demo application (two simple screens) where the microphone and camera are enabled/disabled by publishing/unpublishing new tracks. I used this approach and not the canonical one (with track unmute/mute) to highlight the memory leaks problem.
The problem can be replicated by turning the camera on/off multiple times. On every camera switch the memory will increase significantly.
Demo Repository: https://github.com/VatamanuBogdan/livekit.playground/tree/master
Commit: d1c2b043d375a44a63e0df373e531e9dc02cefcd
Steps:
The entire camera-enabling logic is placed inside ConferenceScreenViewModel.setCamera method.
Code
ConnectOptions
RoomOptions
An important thing to notice is that the following room's option changes increased the amount of leaked memory
from 3 - 4MB to 50MB: VatamanuBogdan/livekit.playground@d16c937
Expected behavior
After the camera track unpublish the memory should decrease but it remains the same.
Screenshots
Application Screens
Memory Usage
Every memory increase represents a camera enabling.
The text was updated successfully, but these errors were encountered: