1

这是我的代码。我在一个干净的新 Xcode 项目中使用这样的代码,内存使用是稳定的。但在我公司的应用程序中,内存消耗每 7 秒左右不断增长,最终应用程序崩溃。

我使用模拟视频缓冲区处理算法

for(int volatile  i = 0; i < 100000000; i++){}

我将 setAlwaysDiscardsLateVideoFrames 设置为 YES。

奇怪的事情: 1.在测试应用程序中,内存保持正常。2.在我公司的应用中,内存在iPhone5s(8.3)上一直在增长,而在iPhone5(8.2)上没有。3.去掉didOutputSampleBuffer中的for循环,我公司的应用也能正常运行,没有内存上升。

所以总而言之,如果我在缓冲区回调函数中执行耗时的算法,并且仅在我公司的应用程序中,内存会不断增长。在带有活动监视器的仪器上,我可以看到内存在增长。但是我看不到分配工具的分配详细信息。看起来像是 CG 栅格数据。

-(BOOL) setupAVCapture{
    session = [AVCaptureSession new];
    [session setSessionPreset:AVCaptureSessionPreset640x480];

    AVCaptureDevicePosition desiredPosition = AVCaptureDevicePositionFront;

    for (AVCaptureDevice *d in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
        if ([d position] == desiredPosition) {
            [session beginConfiguration];
            AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:d error:nil];
            for (AVCaptureInput *oldInput in [ session inputs]) {
                [ session removeInput:oldInput];
            }
            [ session addInput:input];
            [ session commitConfiguration];
            break;
        }
    }

    // Make a video data output
    videoDataOutput = [AVCaptureVideoDataOutput new];

    // we want BGRA, both CoreGraphics and OpenGL work well with 'BGRA'
    NSDictionary *rgbOutputSettings = [NSDictionary dictionaryWithObject:
                                       [NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
    [videoDataOutput setVideoSettings:rgbOutputSettings];
    [videoDataOutput setAlwaysDiscardsLateVideoFrames:YES]; // discard if the data output queue is blocked (as we process the still image)
    videoDataOutputQueue = dispatch_queue_create("MYFDVideoDataOutputQueue", DISPATCH_QUEUE_SERIAL);
    [videoDataOutput setSampleBufferDelegate:self queue:videoDataOutputQueue];

    if ( [session canAddOutput:videoDataOutput] )
        [session addOutput:videoDataOutput];
    [[videoDataOutput connectionWithMediaType:AVMediaTypeVideo] setEnabled:YES];

    [session startRunning];

    return YES;
}

#pragma mark delegate

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
    CFRetain(sampleBuffer);
    for(int volatile  i = 0; i < 100000000; i++){}
    CFRelease(sampleBuffer);
}
4

0 回答 0