GPUImagePicture memory issue
I have been using this library and it is impressive by all standards. I am able to build a full blown editing app using GPUImage and OpenCV. Till time i am able to process images upto 5 MPs to 8 MPs and real time performances on images upto 1.25-2 MP. Though i am facing a big problem with memory and unable to overcome this. Any advice in this regard will be of great help and will be thankful.Here is the scenario:
- User takes one image from gallery.
- Then he goes and does some editing.He applies a filter like curve tones.
- Now he saves this image.
- Now he edits that image through some opencv filter like inpainting.
- After that he applies on this image.
- Now he want to again edit this image. I want to set this new image in GPUImagePicture.
- Everytime i set a changed images in GPUImagePicture there is a big memory leak. How to overcome this?
Even in simple cases where user selects an image and if he go on to choose another image from gallery and again GPUImagePicture is set and there is memory leak.
So even if user just changes the image from gallery and I set GPUImagePicture memory keeps on leaking. There should be some mechanism to reset GPUImagePicture memory. I am unable to figure this out.
I have even written a custom method in GPUImagePicture but it release some portion of memory and not the whole memory.
-(void)clearPictureSource{ if(outputFramebuffer){ [outputFramebuffer destroyFramebuffer]; } }
Is there anyway to reset the whole context and release all memory GPUImage framework is taking?
Really appreciate any help on this. Thanks in advance.
So essentially there should be a way to reset the whole GPUImage thing so that all memory is freed. What is the best possible way out? Otherwise just setting the GPUImagePicture keeps on accumulating memory. Thanks for any suggestion
Did you ever figure this out? I'm encountering a similar problem
I couldn't work on this problem. I will be working on this next week. I will surely update you about my findings.
Thanks, Sharad
On Thu, Feb 25, 2016 at 1:31 AM, Johan Ismael [email protected] wrote:
Did you ever figure this out? I'm encountering a similar problem
— Reply to this email directly or view it on GitHub https://github.com/BradLarson/GPUImage/issues/2191#issuecomment-188431872 .
Great thank you, I'll update you as well if I find something. Kind of a big bummer :/
Got any news? I'm having great result using GPUImageAdaptiveThresholdFilter for binarizing images prior to OCR pass, but after like 2 simple calls , I get a big spike in memory usage and never released back.
On iPhone 6, i got to 50 MB, then 80 MB, with 120 spikes, but from there it stays the same. If it was a continuos leak, it would grow continuously, instead it looks like it reach a plateau. The problem is with older devices (4S crashing) and bigger cameras (6 Plus crashing too).
Instruments of course gives no leak whatsoever.
Not yet buddy. I also observed that instruments gives no leak. Working on it. Will update you if i reach any conclusion.
On Mon, Mar 14, 2016 at 2:42 PM, klauslanza [email protected] wrote:
Got any news? I'm having great result using GPUImageAdaptiveThresholdFilter for binarizing images prior to OCR pass, but after like 2 simple calls , I get a big spike in memory usage and never released back.
On iPhone 6, i got to 50 MB, then 80 MB, with 120 spikes, but from there it stays the same. If it was a continuos leak, it would grow continuously, instead it looks like it reach a plateau. The problem is with older devices (4S crashing) and bigger cameras (6 Plus crashing too).
Instruments of course gives no leak whatsoever.
— Reply to this email directly or view it on GitHub https://github.com/BradLarson/GPUImage/issues/2191#issuecomment-196214183 .
Just to keep things clean, I've created a test project, only with GPUImage. I've put only a UIButton that calls a filter on an asset image, and saves it to disc.
- (IBAction)binarize:(id)sender
{
@autoreleasepool {
UIImage *inputImage = [UIImage imageNamed:@"scontrino"];
[[GPUImageContext sharedFramebufferCache] purgeAllUnassignedFramebuffers];
// Initialize our adaptive threshold filter
GPUImageAdaptiveThresholdFilter *stillImageFilter = [[GPUImageAdaptiveThresholdFilter alloc] init];
stillImageFilter.blurRadiusInPixels = 8.0; // adjust this to tweak the blur radius of the filter, defaults to 4.0
// Retrieve the filtered image from the filter
UIImage *filteredImage = [stillImageFilter imageByFilteringImage:inputImage];
[stillImageFilter removeAllTargets];
[stillImageFilter endProcessing];
stillImageFilter = nil;
[[GPUImageContext sharedFramebufferCache] purgeAllUnassignedFramebuffers];
NSString *newImagePath = [[self randomTempFilePath] stringByAppendingString:@".jpg"];
NSData *imageData = UIImageJPEGRepresentation(filteredImage, 0.8);
[imageData writeToFile:newImagePath atomically:NO];
}
}
On first pass, I go to 40 MB. On second one i got around 70 MB. From now on, successive calls stay on the same level (some fast calls get a little spike, check images). That base memory is never released. Looks like GPUImage keeps a reference to some type of memory buffer that never gets released. (In the image, it is the first and second step).
Without aureleasepool I get the same results.

Even with news commits the situation has not improved. The work done in this library is impressive, but I can't sort out these leaks so unable to use in production :-(
See this solution. Today i found. Very simple but it was very difficult work to find. 😂 https://github.com/metasmile/GPUImage/commit/186a06fe59b9804a735acd386681e8f525d04e3e
Can add this code in the object dealloc [[SLGPUImageContext sharedFramebufferCache] purgeAllUnassignedFramebuffers];