Apps under iOS receive low memory warnings via these mechanisms:
-
[AppDelegate applicationDidReceiveMemoryWarning:] -
UIApplicationDidReceiveMemoryWarningNotification -
[UIViewController didReceiveMemoryWarning]
What are the relationships between these items? Do they all occur when a low memory condition is received or do they have subtle differences? In what order are they executed?
In the simulator there is an option to “Simulate memory warnings”. Does this do anything except for called didReceiveMemoryWarning: on all UIViewControllers?
I’m wanting to use the Xcode profiler tool to see what happens these days around images loaded by the caching [UIImage imageNamed:] method — are they uncached? — but I need to know how to ‘properly’ trigger low memory conditions — even if it’s just by allocating a whole lot of memory.
In answer to your question about the
UIImagecache forimageNamed, yes, it appears that it does purge the cache. Here I loaded 225 images viaimageNamedand then, 20 seconds into the execution, I simulated a memory warning:Having said that, I think the
imageNamedcache is a blunt instrument, and I prefer to do my own cache viaNSCache, so I can constrain how many images it should retain in the cache by settingcountLimit, with the goal of preventing a memory warning at all. As an aside, whileNSCachedoes not respond toUIApplicationDidReceiveMemoryWarningNotification, it does automatically purge itself in real low memory situations. It will not, though, respond to manually simulating memory warning in the simulator.Anyway, here is the log of loading images with a
NSCache, first without thecountLimitand secondly with acountLimitof 50, purging the cache at the end of each: