dimitris-c / AudioStreaming

An AudioPlayer/Streaming library for iOS written in Swift using AVAudioEngine.
MIT License
266 stars 56 forks source link

Memory used is constantly increasing when playing podcasts #28

Closed stefanospapa closed 2 years ago

stefanospapa commented 3 years ago

We noticed this issue on our application but also in the example in GitHub. Trying to play "Swift by Sundell" podcast and then again "Swift by Sundell" the memory used is constantly increasing. It seems that the allocated memory (200MB for this audio item) is not released. This behaviour does not affect live streaming audio like radio stations.

dimitris-c commented 3 years ago

Unfortunately this is a known issue, and I've been trying to find a way around this but with no luck so far.

Keep in mind that the memory is released once the podcast stops playing, do not trust the Xcode's memory graph on the left pane. If you Profile the app, use the Instruments app to check the memory, you will see that while the memory does increase it goes away after you stopped the audio or play another audio file.

Some more context: RemoteAudioSource uses OperationQueue to "store" the audio packets and sends those when requested by the player. This causes the memory to increase since it temporary stores those data in memory. I tried multiple things to try and resolve this but it was always the same outcome. The original library uses the deprecated (since iOS 9!) CFReadStreamCreateForHTTPRequest which doesn't have this issue but there's hasn't been a direct replacement for this API.

stefanospapa commented 3 years ago

Observing the allocations in the instruments app we do not see any releases. Memory is increasing constantly even if we stop the podcast and play a radio stream. Do we monitor wrong data?
Our main concern is if this issue will prevent the app to successfully pass the Apple evaluation. Are there any other apps that use this library and manage to be on the Apple store?

dimitris-c commented 3 years ago

@stefanospapa Hmm, it seems you're right.

Can you try out this branch and see if this fixes the leak issue? https://github.com/dimitris-c/AudioStreaming/tree/bug/remoteaudiosource-memory-leak-fix

If you could test this with your app, especially the seek functionality and report that everything is working as expected I would appreciate it and make a release.

PR #28 for this fix.

stefanospapa commented 3 years ago

We tested your fix in the example application and in our application. It seems OK. Seek operation works normally.
You may proceed with the release. Thanks a lot for the support.

dimitris-c commented 3 years ago

Release is out - https://github.com/dimitris-c/AudioStreaming/releases/tag/0.7.0

Thanks for identifying and QA'ing this issue

stefanospapa commented 2 years ago

During some additional tests we spotted another sub issue of this problem. When we play a podcast like "Swift by Sundell" and pause the player, and then we play a radio station, memory is not released. We have to stop the podcast playing in order to let memory be released. Is there any way to "clear" the player before starting a new playing session? Or any other solution?

dimitris-c commented 2 years ago

Oh that's a good catch — will make a PR a bit later @stefanospapa

dimitris-c commented 2 years ago

This should be fixed now — Update version to 0.8.0 as well, let me know if this fixes the issue

stefanospapa commented 2 years ago

Version 0.8.0 fixes the issue. Thanks for your support.