views:

56

answers:

3

What ways are there to deal with memory issues on the iPhone?

Is it possible to ask how much memory is available before jumping into a memory intensive section of code?

(or perhaps Apple would just say that if you have to use so much memory you are on the wrong platform?)

A: 

Put the following in your app delegate and it will be called when memory is starting to run low. This is the Apple way of doing things:

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    // Free some memory or set some flag that we are low
}
Matt Williamson
But what if I did know how much memory I need beforehand? The method you mention above is called after things go south.
John Smith
no, it's called just before things go south. you might want to abort the method in mid flight at this point. you can't tell how much memory usage the iOS will jettison your application at. you can only conform using this method.
Matt Williamson
applicationDidReceiveMemoryWarning is often sent after things go South. It's sent when some other process wants the memory, and your app might get killed if you don't give it up. If you've already filled up memory with some huge partially-modified photo or somesuch for which the user will kill you if it's lost, you're dead. (I suppose you could try to write it out to some huge file and reading it back in later, but that's real hard to do half way though some recursive filter or somesuch.)
hotpaw2
+3  A: 

UIApplicationDelegate's applicationDidReceiveMemoryWarning: will let you know if you're using too much memory. If you want to check before a memory intensive operation, here's a function that gets the available free memory in bytes on iOS:

natural_t  TSGetFreeSystemMemory(void) {
    mach_port_t           host_port = mach_host_self();
    mach_msg_type_number_t   host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
    vm_size_t               pagesize;
    vm_statistics_data_t     vm_stat;

    host_page_size(host_port, &pagesize);

    if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS) 
        printf("failed to get host statistics");;

    // natural_t   mem_used = (vm_stat.active_count + vm_stat.inactive_count + vm_stat.wire_count) * pagesize;
    natural_t   mem_free = vm_stat.free_count * pagesize;

    return mem_free;
}
Art Gillespie
+2  A: 

Apple appears not to be telling developers because they want to change the amount of memory available in new devices and OS releases. The number went way up on a freshly booted iPhone 4 and way down under iOS 4.0 after typical use on an iPhone 3G.

One possible method is to "preflight" the memory required for successful completion of some operation (e.g. malloc, check and then free the blocks that add up to your requirement). You can malloc in small chunks using a timer spanning many milliseconds to see if you can "push" other apps out of memory. But even this method is no guarantee, as Mail or some other background app could jump in and consume memory even when your app is frontmost.

If you use less than 20MB at any point in time, then a huge percentage of games in the App store will fail before your app does (just my random opinion).

hotpaw2