问题描述:

I am using this code to generate 5 blur images using GPUImage and it seems like there is a memory accumulation of about 20MB which never gets released. Am I doing something wrong?

Here is my code:

dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

GPUImageFastBlurFilter *blurFilter = [[GPUImageFastBlurFilter alloc] init];

GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:[image copy]];

[stillImageSource addTarget:blurFilter];

CGFloat maxBlur = 12.0;

for (int i=0; i < BLUR_STEPS; i++) {

if (!self.stopBlurOperation) { //stops blur operation on close

UIImageView *imageView;

if (i < self.blurredImageViews.count) {

imageView = (UIImageView *)self.blurredImageViews[i];

blurFilter.blurRadiusInPixels = maxBlur * (i+1) / BLUR_STEPS;

[blurFilter useNextFrameForImageCapture];

[stillImageSource processImage];

UIImage *blurredImage = [blurFilter imageFromCurrentFramebuffer];

dispatch_async( dispatch_get_main_queue(), ^{

[imageView setImage:blurredImage];

});

blurredImage = nil;

}

}

}

[blurFilter removeAllTargets];

[stillImageSource removeAllTargets];

[GPUImageContext setActiveShaderProgram:nil];

blurFilter = nil;

stillImageSource = nil;

});

网友答案:

First, you appear to be using an old version of the framework, as GPUImageFastBlurFilter hasn't existed in there for months. The latest code from the repository uses a new framebuffer caching memory model, which is significantly more efficient in most applications.

Second, that's an extremely inefficient way to run multiple blur passes. Going to and from UIImages requires transferring data to and from the GPU, which is slow, and also requires redrawing using Core Graphis, which is even slower. Again, the code in the framework from the last several months has efficient means of generating large-radius blurs without any artifacting you may have seen before, making the above loop unnecessary.

Finally, you're running a tight loop in the above, and generating at least one autoreleased UIImage at each pass in the loop. Without an autorelease pool to drain in there somewhere, you're going to keep building those up in memory while that loop runs. However, as I said, you can remove all of this and not worry about the memory accumulation if you just update to the latest code in the repository.

相关阅读:
Top