MemoryManagement-Collection of common programming errors
Does iphone support GC(Garbage Collection)?
No.
As GC does the same thing that auto release does i.e. it releases its memory at its own time
They don’t quite do the same thing. Autorelease is very specific about the time it will release the object: you know exactly when it will happen.
Sending autorelease to an object will release the memory the next time the current autorelease pool gets drained. An autorelease pool is a thing that keeps track of the objects that have been sent autorelease.
By default, there is only one autorelease pool, and it is drained (i.e. all its objects are released) each time round the run loop.
The run loop works thus. Each time an event happens (such as a timer firing, or the user interacting with the app), the run loop works out which object should handle the event and calls the appropriate method. When that method returns, the run loop moves on to the next event, but before doing so it drains its autorelease pool. (That’s a bit of a simplification but close enough to understand the concept.)
What does that mean in practice? (I hear you cry.) It means that if you autorelease an object in a certain method, the object won’t actually be sent release until that method returns, and the method that called it returns, and the method that called that, and so on all the way to the top. That’s why it’s used a lot when returning objects from methods: you know they’ll persist long enough for the caller to evaluate them (and retain them if needed).