Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS low memory warnings and simulator's "Simulate memory warning"

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.

like image 416
occulus Avatar asked Feb 10 '26 21:02

occulus


1 Answers

In answer to your question about the UIImage cache for imageNamed, yes, it appears that it does purge the cache. Here I loaded 225 images via imageNamed and then, 20 seconds into the execution, I simulated a memory warning:

allocations for imageNamed

Having said that, I think the imageNamed cache is a blunt instrument, and I prefer to do my own cache via NSCache, so I can constrain how many images it should retain in the cache by setting countLimit, with the goal of preventing a memory warning at all. As an aside, while NSCache does not respond to UIApplicationDidReceiveMemoryWarningNotification, 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 the countLimit and secondly with a countLimit of 50, purging the cache at the end of each:

NSCache without and with countLimit

like image 117
Rob Avatar answered Feb 13 '26 13:02

Rob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!