monkey_rjpan
monkey_rjpan

Reputation: 11

iOS AVPlayer play a lot videos and cause memory too high

First of all, thank you for your help.

The Questions:

1.I need to test a lot videos.I wrote two methods to test videos.

2.When playing a video status change, and I set avPlayer = nil and avPlayerItem = nil, i also add the @autoreleasepool {}, but the memory is slowly increased, I used the instruments but I did not find memory leaks, I find a lot questions and did not find a solution.

3.Here is my code:

-(void)CheckVideoURlCanPlay{
@autoreleasepool {
    VideoPlayDataObj *videoPlay = [self.needCheckArr objectAtIndex:0];
    NSString *str = videoPlay.videoURL;
    NSURL *url = [NSURL URLWithString:str];
    self.playItem = [AVPlayerItem playerItemWithURL:url];
    self.player = [AVPlayer playerWithPlayerItem:self.playItem];
    self.playLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    self.playLayer.videoGravity = AVLayerVideoGravityResizeAspect;
    self.playLayer.frame=CGRectMake(100, 80, 100, 100);
    [self.view.layer addSublayer:self.playLayer];
    [self.player play];
    [self.playItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
   }}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
@autoreleasepool {

    if ([keyPath isEqualToString:@"status"]) {
        if (self.player.currentItem.status == AVPlayerItemStatusReadyToPlay) {
            @autoreleasepool {

                [self.player.currentItem.asset cancelLoading];
                [self.player.currentItem cancelPendingSeeks];
                [self.player cancelPendingPrerolls];
                [self.playItem removeObserver:self forKeyPath:@"status"];
                self.playItem = nil;
                self.player = nil;

            }
            [self.needCheckArr removeObjectAtIndex:0];
            [NSThread sleepForTimeInterval:5];
            [self CheckVideoURlCanPlay];
        }
    }
}

}

4.I also tried to relieve the current view, but it does not relieve all. If you play a large number of video memory will be high, the final app will be killed.

5.I wonder if after loading a video, it will produce dirty memory?

this my app start memory:13M this test some videos and i pop the view : enter image description here this test some videos again and pop the view : enter image description here

6.Finally, I hope you can help me, thank you very much.

Upvotes: 1

Views: 2402

Answers (1)

damian
damian

Reputation: 3674

It does not look like you are ever removing the AVPlayerLayer from the view:

[self.playerLayer removeFromSuperlayer];

Also I cannot see the point of @autoreleasepool here, especially if you have ARC turned on (which you almost certainly do!). @autoreleasepool is usually only useful when you have ARC turned off, or when you are churning through a lot of memory (as in, many megabytes) in a single main-loop invocation and need to control when they get cleaned up.

Upvotes: 2

Related Questions