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

Huge memory leaks produced by publishing/unpublishing camera tracks #420

Open
VatamanuBogdan opened this issue Jul 2, 2024 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@VatamanuBogdan
Copy link

VatamanuBogdan commented Jul 2, 2024

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:

  1. Login into your room using the form found on the Login Screen (you can hardcode the room URL and Token by modifying the values of defaultServerURL and defaultToken constants that can be found at LiveKit-Playground/Utils/Constants.swift )
  2. After you log in with success the Conference Screen will appear and then you should enable -> disable camera by using the Camera toggle button
  3. On every enable -> disable repetition you will see that memory increases drastically see

The entire camera-enabling logic is placed inside ConferenceScreenViewModel.setCamera method.

Code

  1. Initialize a room with the following connect and room options

ConnectOptions

let connectOptions = ConnectOptions(autoSubscribe: false, protocolVersion: .v9)

RoomOptions

let cameraCaptureOptions = CameraCaptureOptions()
let audioCaptureOptions = AudioCaptureOptions()
let videoPublishOptions = VideoPublishOptions(encoding: VideoEncoding(maxBitrate: 2_000_000, maxFps: 30),
                                                                                  simulcast: false,
                                                                                  preferredCodec: VideoCodec.vp8)
let audioPublishOptions = AudioPublishOptions()

let roomOptions = RoomOptions(
          defaultCameraCaptureOptions: cameraCaptureOptions,
          defaultAudioCaptureOptions: audioCaptureOptions,
          defaultVideoPublishOptions: videoPublishOptions,
          defaultAudioPublishOptions: audioPublishOptions,
          adaptiveStream: false,
          dynacast: false,
          reportRemoteTrackStatistics: true
  )
  1. Connect to room
let room = try await room.connect(url: <Server URL>,
                                                          token: <Token>,
                                                          connectOptions: connectOptions,
                                                          roomOptions: roomOptions)
  1. Create and publish a new camera track
let videoTrack = LocalVideoTrack.createCameraTrack()
try await localParticipant.publish(videoTrack: videoTrack)
  1. Unpublish camera track
let cameraPublication = room.localParticipant.firstCameraPublication as! LocalTrackPublication
try await self.localParticipant.unpublish(publication: cameraPublication)
  1. Repeat steps 2 and then 3 several times and you will see that memory increases with every repetition

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

Login Screen Conference Screen
login screen conference screen

Memory Usage

Memory-Usage-Screenshot

Every memory increase represents a camera enabling.

@VatamanuBogdan VatamanuBogdan added the bug Something isn't working label Jul 2, 2024
@hiroshihorie
Copy link
Member

Thanks for the detailed report, will investigate this.

@hiroshihorie
Copy link
Member

Hello I do see huge memory leaks, I did a quick investigation but I can't find where at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants