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
The NSCache object removes its data basing on its own rules. That doesn't mean that it will release content during a memory warning.
Here what the doc states:
The NSCache class incorporates various auto-eviction policies, which ensure that a cache doesn’t use too much of the system’s memory. If memory is needed by other applications, these policies remove some items from the cache, minimizing its memory footprint.
Most probably the changed some policies in iOS7. You can remove all contents by listening to memory warning notification. I link this answer for sake of completeness.
While NSCache
never responded to memory warnings, I found that it generally responded to true memory pressure. The failure to respond to memory warnings has always been a bit of an annoyance (e.g. you couldn't just use the "simulate memory warning" to test the behavior of an app in memory pressure).
Having said that, I see the same behavior you describe. iOS 7 seems to have changed the NSCache
behavior.
Personally, I just have simple-minded NSCache
subclass that just removes all of the objects upon receiving the UIApplicationDidReceiveMemoryWarningNotification
notification:
@implementation AutoPurgeCache
- (id)init
{
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeAllObjects) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
}
@end