MEMORY MANAGEMENT FOR IOS


                                                           MEMORY MANAGEMENT


Modern computer languages uses Garbage Collection…
For latest versions there is no need of deallocating the memory .
It will automatically deallocates the memory.....

A Garbage Collector is a runtime algorithm that scans the allocated Objects in our Program, and Deallocates the Objects that we have loss the Contact.
Cocoa under the iphone Operating System doesn't use Garbage Collection..so,Cocoa apps must use Managed Memory applications running on this platform should CleanUp after the use..
Keywords for the MemoryManagement :--
(A) retain
(B) release
(C) autorelease
(D) drain
(E) copy
(F) NSAutoReleasePoolClass
(G) dealloc method

(A) retain:

This specifies that you are interested in putting an ownership claim on this object. If the caller release this object it does not deallocated.The compiler will invoke a retain on this object and assign it to the instance variable. If the caller later released this object, it does not get deallocated since you retained it. You need to release it, either when you are finished with it, or in the dealloc method of your object.
it increases the retain by 1.
syntax:
[obj retain];
(B) release: We call release when we are done using an object. We are no langer the owner of the object.so,the reference count is '0'.(no owners left) then the object is automatically deleted. then the memory is freed. otherwise, the object space in memory for other owner to use.
syntax:
[obj release];
© AutoRelease: This means that you need the object temporarily,and doesn't make to use as like owner.
It is just like you as a temporary user it just like the same as that object now keep it in memory while I do few thing with it then you can delete.
Ex: NSString *str=[NSString StringWithString:"hai"];

(d) drain:When we pass drain for pool object it deallocates the all the objects in a pool.
Ex:[pool drain];
(e) Copy:
This attribute creates copy of the object. When we call setter method and then you become owner of the object.
You use this attribute when you want to store a copy of the object being assigned to your instance variable rather than the reference to that object. The value being assigned has to be an object that knows how to copy itself (i.e., implements the NSCopying protocol).
(f)AutoReleasePool:
An autoreleasepool is an instance of NSAutoreleasepool class, and define the scope for temporary objects.[objects which are to be auto released].
Any objects which are to be autoreleased
[(objects we sends the autorelease message)
(or)
(create with convinience methods)] are added to the correct autorelease pool.
When the autoreleasepool is realized all objects that were added to it are also automatically released.
This is simple way of managing automatic release for objects which are needed temporarily. Auto release pools can be nested. In this case all autorelease pool objects are added to the latest pool. the pools are stored in a stack.
NSAutoReleasePool *pool=[[NSAutoReleasePool alloc]init ];
NSString *str=[NSString StringWithString:@"hello"];
[pool release]; 


0 comments:

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Blogger Templates