Objective C 2.0 introduces garbage collection to take care of memory.
In COCO there is autorelease pool .ie class name NSAutoreleasePool. It holds the objects(collection of objects) that automatically gets released.
NSObject supreme base class of all objective C class has
- (id) autorelease;
method. When we call autorelease on object it means the release message is sent to object in sometime future.When call to autorelease method particular object is get added to NSAutoreleasePool. When that pool is destroyed all the object contain in that pool gets release method.
When you are using AppKit , COCO automatically creates and destroys an autorelease pool on a regular basis.
Important the functions like
usually return the object with autorelease called on it so we do not require to take care for free that memory.
Some objects are like singleton
ARC VS Garbage collection
Reference Counting/Retain counting:-
Every object has an integer with it called reference count or retain count. Which keep count of that object are in use. When you are giving object to some class/ code and used retain when giving then retain/reference count is increased by one.
Eg.
-(NSString*) name
{
return [name retain];//here name reference count is increased by one
}
When that class done with that object then it calls release on it it decrement reference / retain count by one. When reference count reduces to 0 (Zero) that objects get freed from memory (by sending dealloc message to it). System gets that memory.
Object creation through alloc/ new/copy set this count 1.
Declaration of retain, release
- (id) retain;
- (void) release;
- (unsigned) retainCount;
Never call dealloc manually system calls it automatically when count goes to zero.
Autorelease(pool to hold objects temporarily when pool destroy release all objects)
In COCO there is autorelease pool .ie class name NSAutoreleasePool. It holds the objects(collection of objects) that automatically gets released.
NSObject supreme base class of all objective C class has
- (id) autorelease;
method. When we call autorelease on object it means the release message is sent to object in sometime future.When call to autorelease method particular object is get added to NSAutoreleasePool. When that pool is destroyed all the object contain in that pool gets release method.
When you are using AppKit , COCO automatically creates and destroys an autorelease pool on a regular basis.
+ (id)stringWithFormat:(NSString *)format, ...
usually return the object with autorelease called on it so we do not require to take care for free that memory.
Some objects are like singleton
NSColor *color= [NSColor blueColor];
Any Ui application is in event loop , continuous waiting for the input.
Cocoa creates an autorelease pool before it starts handling the event and destroys the pool after the event is handled. So in this case u have to take care of the object u kept in autorelease pool and which u require after the completion of events. eg + (id)stringWithFormat:(NSString *)format, ...
object created with this method at time of event , will live after event .
A best example of custom autorelease pool take from book Learning Objective C on the Mac By Mark Dalrymple | Scott Knaster
in below example the pool maintain so that memory will not reach at certain level.
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
int i;
for (i = 0; i < 1000000; i++) {
id object = [someArray objectAtIndex: i];
NSString *desc = [object descrption];
// and do something with the description
if (i % 1000 == 0) {
[pool release];
pool = [[NSAutoreleasePool alloc] init];
}
}
[pool release]
After every 1000 time pool is released so that memory will be available for next time to use.
Autorelease pools are kept as a stack: if you make a new autorelease pool, it gets added to the top of the stack. An autorelease message puts the receiver into the topmost pool. If you put an object into a pool, and then make a new pool and destroy it, the autoreleased object will still be around, because the pool holding that object is still in existence.
Garbage Collection objective C 2.0 onwards
Its same like as Java just create object use it , do not worry about its destruction . Garbage collector will find the use of object and it will automatically destroy the object. Developer has no need to worry abt it.
Like the autorelease pool, garbage collection is triggered at the end of an event loop.
ARC VS Garbage collection
ARC is not a Garbage Collector. It is better to think of it as manual reference counting (retain/release/autorelease) calls which are added by the compiler. It also uses some runtime tricks.
OS has no method of Garbage Collection. Even so, Garbage Collection is entirely unnecessary (for all practical purposes) when ARC is used. ARC works its magic at compile time to do the reference counting for you thereby making it unnecessary (and actually non-allowed) to use any other sort of memory management.
0 comments:
Post a Comment