views:

152

answers:

4

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.

A: 

I 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.

Dave DeLong
You're assuming that the Mac's 1GB of RAM is available for use. If other applications are using 99% of the RAM, then your application has only 1% of RAM available for its own use.You could say there is less need for the feature on a Mac, but that's not the same as no need.
Jeremy Friesner
@jeremy Memory on desktop systems doesn't really work that way. @Dave Actually, there is considerable need to manage memory efficiently and it isn't uncommon for an application to tune memory usage to the capabilities of the system to achieve maximum performance. Fortunately, there is an API for that (in Snow Leopard).
bbum
+2  A: 

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.

Darren
+6  A: 

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.

bbum
Thank you Bill. I'm indeed trying to create a cache of data.. images in this case, which the system can reclaim if it needs. The NSDiscardableContent protocol sounds like exactly what I'm looking for. Great to see built into the system. Yay for Snow Leopard!
Harry Jordan
Also have a look at NSCache...
bbum
A: 

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.

Mark Bessey