CodexBloom - Programming Q&A Platform

Issues with High Memory Usage in macOS 13.6 App When Using AVPlayer with Custom Playback Controls

πŸ‘€ Views: 14 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-13
AVPlayer macOS memory-management Swift

I've looked through the documentation and I'm still confused about Does anyone know how to I'm encountering significant memory usage in my macOS 13.6 application that utilizes `AVPlayer` for video playback, especially when I implement custom playback controls. The app's memory footprint increases dramatically over time, and profiling it shows that the memory is not being released properly. I've set up my `AVPlayer` instance like this: ```swift import AVKit class VideoPlayerViewController: NSViewController { var player: AVPlayer! var playerLayer: AVPlayerLayer! override func viewDidLoad() { super.viewDidLoad() let url = URL(string: "https://path/to/video.mp4")! player = AVPlayer(url: url) playerLayer = AVPlayerLayer(player: player) playerLayer.frame = self.view.bounds self.view.layer = playerLayer player.play() } } ``` While streaming, I added custom controls for play, pause, and scrub functionality. The controls interact with the `AVPlayer` instance like so: ```swift @IBAction func playButtonTapped(_ sender: NSButton) { player.play() } @IBAction func pauseButtonTapped(_ sender: NSButton) { player.pause() } @IBAction func scrubSliderChanged(_ sender: NSSlider) { let duration = player.currentItem?.duration.seconds ?? 0 let newTime = duration * (sender.doubleValue / 100) player.seek(to: CMTime(seconds: newTime, preferredTimescale: 600)) } ``` However, when I run the app for extended periods, I notice that the memory usage climbs and doesn't seem to drop after playback stops. Instruments shows that there are lingering references to instances related to the player, even after I try to deallocate them. I suspect it might be related to how I'm handling the player and its observers. To troubleshoot, I've tried manually removing observers like this: ```swift deinit { player.removeObserver(self, forKeyPath: "status") } ``` But I still see high memory usage. The console log doesn’t show any obvious errors, and I don’t know how to further diagnose the root of the memory issues. Any insights into what I might be missing or how to better manage memory with `AVPlayer` would be greatly appreciated. What am I doing wrong? I've been using Swift for about a year now.