views:

402

answers:

2

I'm reading up on the leak-finding tools for iPhone development and intentionally inserting and looking for memory leaks in my small program before I get into a bigger, harder-to-debug project.

It's no problem to identify a memory leak that, say, occurs in a regularly invoked method like responding to a touch event. The leak instrument will eventually identify virtual memory that is unclaimed.

I'm more worried about a leak in code for exiting the entire system. Once I completely exit my app, is it a concern that something wasn't deallocated, or does iphone OS automatically reclaim all user mem at that point?

This issue is unclear to me after reading quite a bit of documentation, and without knowing anything else, I assume it must work like other OS's in that regard and just take back all user space. If that's so, won't I be fine cleaning up regular leaks so my app can run for any amount of time with bounded memory, then not worry so much that everything gets freed up at exit?

Also, if it is critical to free everything before exiting because it will not be reclaimed by the OS, is there a good way to keep my app alive in the instruments after exit for inspection? When I press the home button in the simulator or on the device haven't I already lost the chance to detect exit-time leaks?

+3  A: 

Yes, ending your program will release every bit of memory held by it. Anything otherwise is an OS bug, and you're unlikely to find that occurring.

EDIT: I bet your asking this because documentation says "iPhone doesn't support garbage collection." That statement, however, doesn't apply to freeing memory when a program ends. It's only talking about how you have to handle freeing your own memory while your program is running.

Autocracy
I still find it to be a useful programming convention to act like the system won't clean up after you, and make sure you handle all deallocations properly on the way out of your application. It's like using turn signals in a parking lot in that it reinforces a good habit.
Brad Larson
And I intend to, but will Apple reject my app if I have an exit-leak and can't figure out how to find and clean them? But I believe the OS shoudl clean up--I just can't find an explicit statement in their labyrinth of iPhone docs saying that. Thanks everyone!
dim fish
A: 

The iPhone should be using virtual memory, so (theoretically anyways) the OS will clean everything up when a leaky app exits.

Eric Petroelje
No, the iPhone OS doesn't have virtual memory. If there is little RAM remaining, the OS will start killing apps.
Marco Mustapic
@Marco - it doesn't have a page file, but it does use a virtual memory model. They are two different (although easily confused) things.
Eric Petroelje
@Eric, you are right, virtual memory it has, but no page files.
Marco Mustapic