I create a function to assemble images into video file by using AvAssetWriter. There are a few threads in forum about this implementation. I have successfully written the video using AVAssetWriter. My question is not this implementation but about memory consumption. In my case, when I write 4 sec 30FPS video 1024*768, the peak memory usage will be around 300MB. For longer time, 10sec etc. it will crash with memory warning. The problem is that the memory usage is accumulated during the loop which write each image into the video file.After the loop, memory usage will drop back to normal level without leakage.
The following code is one iterate of the loop. It appends a new image to the avassetwriter
CVPixelBufferRef buffer = [self pixelBufferFromCGImage:[newimg CGImage]
size:CGSizeMake(self.frameOrigWidth, self.frameOrigHeight) poolRef:adaptor.pixelBufferPool];
BOOL append_ok = NO;
int j = 0;
CMTime frameTime = CMTimeMake(frameCount,(int32_t)FPS);
while (!append_ok && j < 30) //attemp maximum 30 times
{
if (adaptor.assetWriterInput.readyForMoreMediaData)
{
if(frameCount==0) append_ok = [adaptor appendPixelBuffer:buffer
withPresentationTime:kCMTimeZero];
else append_ok = [adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];
[NSThread sleepForTimeInterval:0.05];//use sleep instead of runloop because it is not in main thread
}else{
[NSThread sleepForTimeInterval:0.1];
}
j++;
}
if(buffer) CVBufferRelease(buffer);
even there is buffer release at the last line, the memory will not be released during the whole loop procedure, I guess it is because the writer retains this buffer till after loop and the writer execute finishwriting.
I tried using @autoreleasepool{} to wrap this part. It effectively stopped the peak memory usage accumulation, but it also does not successfully write video file anymore even there is no running error.
The above explanation is from real device debug.
I thought of possible solution is to segment the writing, or pause a few times during the writing cycle to allow buffer really released by the writer. But i do not find a way to do that. I appreciate anyone who knows the method to solve this peak memory problem.