We are using NSCache for UIImages in our app. This works fine on iOS versions smaller than 7. When a memory warning occurs, NSCache releases objects as intended. However, on iOS 7, our app crashes shortly after the first memory warning. So it seems as if objects stored with NSCache are never released but the cache is growing until the app is crashing. Profiling with instruments confirms this suspicion.
Did somebody else experience this problem and did you find a workaround or already track a bug?
It looks like those guys had the same issue: http://www.photosmithapp.com/index.php/2013/10/photosmith-3-0-2-photo-caching-and-ios-7/
I created a small sample app to illustrate the issue. When a button is pressed, the method -(IBAction)fillCache:(id)sender
is called. From then on, a timer calls -(void)addImageToCache:(id)sender
every 100 ms. In this method, a UIImage is generated and written to cache.
On the iPad Mini with iOS 7.0.3 and its 512 MB memory, it crashes after ~350 iterations.
On the iPad 2 with iOS 5 and also 512 MB memory, it also crashes at some point, but only after at least 3000 iterations. Instruments shows that the number of UIImage instances decreases everytime a memory warning occurs. This is not the case on iOS 7.
- (IBAction)fillCache:(id)sender
{
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(addImageToCache:) userInfo:nil repeats:YES];
}
- (void)addImageToCache:(id)sender
{
@autoreleasepool {
CGRect rect = CGRectMake(0, 0, 500, 500);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSString *poolKey = [NSString stringWithFormat:@"junk_%d", count++];
[self.cache setObject:image forKey:poolKey];
}
}