In my app, there is a welcome screen consist of 3 screens. They all inherit from one super class. The different between them are just texts and video url:
|-SuperWelcomeScreenViewController
|----FirstWelcomeScreenViewController
|----SecondWelcomeScreenViewController
|----ThirdWelcomeScreenViewController
In the super class, I have a view to load view:
private final var player: AVPlayer = AVPlayer()
private final lazy var videoView: UIView = {
let v = UIView()
let videoString:String? = Bundle.main.path(forResource: self.videoPath, ofType: "mp4")
guard let unwrappedVideoPath = videoString else {return v}
let videoUrl = URL(fileURLWithPath: unwrappedVideoPath)
let item = AVPlayerItem(url: videoUrl)
self.player.replaceCurrentItem(with: item)
let layer: AVPlayerLayer = AVPlayerLayer(player: player)
//Using the size of the video
layer.frame = CGRect(x: -125, y: 0, width: 250, height: 541)
layer.videoGravity = AVLayerVideoGravity.resizeAspectFill
v.layer.addSublayer(layer)
return v
}()
Here is the issue: I test the memory usage on Simulator, in the beginning it's about 250MB. Then I swiped to next welcome screen, it became 450MB. Then next for 550MB. My welcome screen has a infinite loop, but the usage stays at maximum after 3 viewcontrollers are all showed.
Then I went to the login screen, and then went back to welcome screen. The initial usage became 750MB. If I keep jumping from the login screen and the welcome screen, the usage will keep raising!
Here are what I want to do:
First, I want memory usage stay steady when I jumping from the login screen and the welcome screen.
Second, I want recycle memory between welcome screens. So that in every screen the usage would roughly be 250MB.
Here are what I have tried and failed:
Change
player
tostatic
Remove
AVPlayerLayer
fromsuperLayer
when view willDisappearSet
AVPlayer
to nil
FYI I ran CFGetRetainCount
in viewDidLoad
and it's 5. I cannot find the other 4.