3

Im fairly new to iOS swift development and Im having issue understanding the memory management.

the app Im working on is a chain of table views ending with a AVPlayerView Controller playing videos from streaming urls.

When running the app in the simulator, and observing the memory dashboard, I can see that coming back from one table view to the previous one makes the memory decreases. However, when entering the AVPlayerView Controller, the memory increases, then when coming back to the previous table View, the memory doesn't decreases.

Im using the following code on my PlayerView Controller (viewDidLoad):

 var player : AVPlayer? = nil
 let playerController = AVPlayerViewController()

 var playerItem = AVPlayerItem(URL: mediaUrl)
            self.player = AVPlayer(playerItem: playerItem)
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem)

            self.playerController.showsPlaybackControls = false

            self.playerController.player = self.player
            self.addChildViewController(self.playerController)
            self.view.addSubview(self.playerController.view)
            self.playerController.view.frame = self.view.frame
            self.view.addSubview(self.scrub)

            self.player!.play()

and this to quit the controller and head back to the previous controller:

            self.player!.pause()
            self.navigationController?.navigationBarHidden = false
            navigationController?.popViewControllerAnimated(true)

Within the player, I user a button to go from one video to another using:

self.player!.replaceCurrentItemWithPlayerItem(playerItem)

using this change the video but doesn't increase the memory. In fact, it decreases a lot, then head back to a normal level so I must assume the memory is linked to the controller and I must have missed something.

I someone could explain how it works it would be very helpful. Maybe it totally normal and I worry for nothing but if it's not I would be glad to fix this. PS: I have to say the app works fine and I don't have any crash or anything.

Thanks

EDIT / UPDATE

Before entering the PlayerView Controller, I usually Im around 25Mo of memory allocated. Once the video play, Im around 125Mo.

I managed to reduce this memory allcoation to around 75 by setting the AVPlayer to nil before dismissing the controller. I also remove the PlayerView layer from it's parent controller but this has no effect on memory.

            self.player!.pause()
            self.playerController.player = nil
            self.playerController.removeFromParentViewController()
            self.navigationController?.navigationBarHidden = false
            navigationController?.popViewControllerAnimated(true)
SKYnine
  • 2,708
  • 7
  • 31
  • 41

0 Answers0