I'm looking for an equivalent to the -(void)applicationDidReceiveMemoryWarning:(UIApplication *)application
method that's available on the iPhone. So far I haven't been able to find anything, but I'd like to check before I start writing my own.
views:
152answers:
4I think you can reasonably expect that most modern Macs will have at least 1GB of RAM (probably 2, and maybe even 4+). If your app is causing malloc errors and/or running out of memory, then you've done wrong and you need to rethink what you're storing in memory.
So in a nutshell, no there isn't. The Mac isn't nearly as memory-constrained as the iPhone, so there's no need for it.
Let the kernel worry about memory management.
The iPhone doesn't use virtual memory, so its memory constraints are very concrete. The only option the kernel has is to warn you before it eventually terminates you. That's why the iPhone has memory warnings.
The desktop kernel has many more options available to deal with low memory conditions, so it's best to just let it do its job. It would do you no good to start aggressively releasing memory that's already been swapped to disk.
Memory on Mac OS X doesn't work quite the same as OS X Touch. Notably, a desktop machine has lots more RAM and will swap memory to disk as apps demand more. As well, there are many applications competing for resources.
The real question is what are you trying to accomplish?
If the answer is use memory efficiently, then you need to focus on minimizing allocations, making sure you have no leaks, and ensuring that your data structures are optimized. Use ObjectAlloc in Instruments to analyze memory usage and figure out where to focus.
However, if the answer is more along the lines of I have a caching subsystem that benefits from lots of memory, but I want to give it back to the system when other apps increase their demands, then you'll want to investigate Snow Leopard's Caching and Purgeable Memory support.
Notably, the two APIs provide a means of aggressively caching data as long as their are system resources, backing off or giving back to the system when their is memory pressure.
There isn't a direct equivalent, as far as I know. You could presumably check whether object allocations were failing, then send a notification to allow other parts of your application could listen for.
In practice, most applications don't bother. In the situations where you'd actually start failing to allocate objects (virtual memory exhausted, startup disk completely full, etc), you're likely headed for a crash anyway.