Question

I was profiling a avfoundation video encoding program. The memory increase as frames get written. After I added a autoreleasepool, the problem went away.

        @autoreleasepool {

            NSString *inputFramePath = self.inputFramePaths[self.currentFrameIndex++];
            self.currentFrame = [UIImage imageWithContentsOfFile:inputFramePath];

            CVPixelBufferRef buffer = (CVPixelBufferRef)[VideoExporter newPixelBufferFromCGImage:self.currentFrame.CGImage size:self.resolution];
            if (buffer) {
                [self.adaptor appendPixelBuffer:buffer withPresentationTime:self.nextPTS];
                CVPixelBufferRelease(buffer);
                self.nextPTS = CMTimeAdd(self.frameDuration, self.nextPTS);
            }
        }

Without autoreleasepool:

enter image description here

With autoreleasepool:

enter image description here

I'm still not very clear about autoreleasepool, can someone explain me what exactly happened in my code? And do I need to batch the frame writing instead of drain the pool for for every frame?

Was it helpful?

Solution

When you autorelease an object, it gets added to an autorelease pool. Usually the pool that is added to is one that is created back up the stack and is drained as part of the run loop (for the one created for you on the main thread) or when the thread exits (when you manually create one at the start of a thread)

By creating an autorelease pool in other places, autoreleased objects in its scoped are released when that pool is drained, rather than waiting for the existing one to drain. In cases where large amount of data is processed in a loop, this can dramatically reduce the high-watermark of memory usage.

Processing through a set of images could possibly be one of such use case. In your case, I'm guessing your code sample is part of a loop to process through several frames at once.

So long as all objects that you need outside of the autorelease pool are retained, then it will be safe to use.

Be sure to read the Advanced Memory Management Programming Guide's section on autorelease pools.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top