1

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.

elephant.lyh
  • 133
  • 1
  • 5
  • The current solution i am using is to separate the writing task to different segments, each segment writes a single file. At the end of the loop, i use AVMutableComposition to combine all small files. By this method, peak memory appears in each segment. Because each segment there are not many imgs to write, the memory will not be consumed too much. I appreciate other ideas – elephant.lyh Dec 13 '13 at 08:27

1 Answers1

0

i have same issue with you

i try use sleepForTimeInterval inside

  • (void)didReceiveMemoryWarning

i was hope when this event triggered, it will pause main process nad give little time to system to flush unused memory

but i dont know if this effective or not

  • Just to let you know that i use the previous mentioned way to solve it. You can split the export video task to many pieces short video generation. Then in the end merge them together. This works fine for me. My app already put on app store, you may have a look at it to see this method working for generating long time video. My app is "Animation DIY" – elephant.lyh Mar 07 '14 at 02:28