开发者

MemoryManagement

Does iphone support GC(Garbage Collection)? If yes then what exactly is auto release.Can any body answer my question pls.

As GC does the same thing that auto release does i.e. it releases its memory at its own time

Eg some functions like stringwithformat are auto release functions and see this

NSXMLParser *xmlParser [[[NSXMLParser alloc] initWithData:xmlData] autorelease];

(xmlparser object also we cant comment that when this objects memory is released) just like GC

开发者_开发知识库

In short what i think is iphone partially supports GC

whts say guys?


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


It sounds like you are mixing up releasing with dealllocing. It is very important to realize that releasing an object does not remove it from memory. Releasing an object tells the runtime that you are done with that object, by decrementing the retain count. When the retain count reaches 0, then the object is actually dealloced from memory. So autorelease is not the same as GC, because it only automatically releases the object, it does not automatically dealloc the object.


iOS does not support Garbage Collection. Garbage collection would remove any objects which no longer have any references to them. On the iOS if you have no references to an object that space in memory will be lost and you'll have a memory leak.

autoRelease tells an object to release in the very near future. This is useful in certain cases, such as returning an object created in a function, where you will not be able to release it.

e.g.:

+ (id) createNewObject {
    id *newObject = [[Object alloc] init];
    [newObject autoRelease];
    return newObject;
}

In this case the object calling this function would have to retain the object that is returned so it is not removed from memory.


No, the iPhone does not support GC.

GC is not the same as auto releasing. Even with auto-releasing you can still have memory management errors i.e. memory leaks and accessing memory thats been released. Releases and autoreleases still have to be balanced with retains/allocs.

Autorelease is there so that methods can create (alloc and init) an object and return them in such a way that the reference count will be zero but the calling method can still access them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜