开发者

NSString or NSArray release?

开发者 https://www.devze.com 2023-03-21 02:57 出处:网络
When I perform a [NSString release] or [NSArray release] or [NSMutableArray release], what happens? Does the memory became wiped out?

When I perform a [NSString release] or [NSArray release] or [NSMutableArray release], what happens? Does the memory became wiped out? Or just pushed on the stack and dropped from the heap, or vise versa?

If I just want to dump memory when I am done with it, is "release" the best thin开发者_开发技巧g to use?

I am dealing with many matrices and don't want them sticking around using memory...

thanks


Just read the Memory Management Programming Guide and your questions will be answered.

Also, if you're dealing with a lot of matrices you may want to use manual autorelease pools.


When I perform a [NSString release] or [NSArray release] or [NSMutableArray release], what happens?

The retainCount of the instance that you call release on is decremented. If it reaches 0 as a result of your call, then the instance will be deallocated.

Does the memory became wiped out? Or just pushed on the stack and dropped from the heap, or vise versa?

Not necessarily. The memory may become wiped out (in the sense that it no longer contains a valid object instance and may be overwritten by other things) once the retainCount reaches 0 (or less if you over-release something), but this is not guaranteed to happen immediately upon your call to release. The released instance may in fact stick around for quite awhile, if it is associated with an NSAutoreleasePool that is not drained frequently or if someone else has called retain on it.

If I just want to dump memory when I am done with it, is "release" the best thing to use?

In general, yes. If you want really low-level control of things, you can also use malloc() and free() instead, which will release memory more immediately than calling release will.


When you perform a release, the use count in the object is decremented. If the use count decrements to zero (because it's not simultaneously "owned" by some other code or data structure), the heap space occupied by the object is marked as available for reuse.

When a new object is allocated, the heap is searched for an appropriately sized piece of reusable space, and if your recently freed piece is the first one found to match the required size, that storage is marked "in use" again and your old data is overwritten with the new object.

Note that this means that if you release an object too soon you may still be able to use it for awhile, but it can suddenly, at any time, go "poof" and turn into an entirely different object, causing mysterious errors.

0

精彩评论

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