1/82
2/82
Memory Management In iOS
3/82
Objective-C
Memory management
Memory management is semi-automatic:
The programmer must allocate memory for objects either
a) explicitly (alloc) or
b) indirectly using a convenience constructor
No need to de-allocate
4/82
Allocation
Allocation happens through the class method alloc. The
message ‘alloc’ is sent to the class of the requested
object. Alloc is inherited from NSObject. Every alloc
creates a new instance (=object)
[HelloWorld alloc];
The class creates the object with all zeros in it and
returns a pointer to it.
HelloWorld *p = [HelloWorld alloc];
The pointer p now points to the new instance.
Now we send messages to the instance through p.
5/82
The reference counter
• Every instance has a reference counter. It counts
how many references are retaining the object.
The counter is 1 after allocation. It does not
count how many references exist to the object
• Sending the retain message to the object
increases the reference counter by 1.
• Sending the release message decreases the
reference counter by 1.
• No need to do either if using ARC.
6/82
reference counter = retain counter
• When the reference counter reaches zero, the
object is automatically de-allocated. The
programmer does not de-allocate.
• The programmer only does:
alloc
retain
release
7/82
Rules for memory management
• With no ARC: The method that does an alloc
or a retain must also do a release, it must
maintain the balance between:
(alloc or retain) and (release)
• If a method does alloc and returns a pointer
to the created object then the method must
do an autorelease instead of release.
8/82
Autorelease pool
• With no ARC: For outorelease to work the
programmer must create an autorelease pool,
using:
NSAutoreleasePool *pool
= [[NSAutoreleasePool alloc] init];
• When Cocoa is used then the autorelease pool is created automatically – the
programmer does not need to do it.
• To release the pool and all objects in it, do:
[pool release];
9/82
Convenience Constructors
• This is a class method that allocates and
initializes an object. The programmer is neither
doing alloc nor init.
• Example:
+(id)studentWithName :(NSString*)name AndGpa:(float)gpa
{
id newInst = [[self alloc]initStudent:name :gpa];
return [newInst autorelease];
}
10/82
Convenience Constructors
• Essential: the method sends alloc to self which
is the Student class object
• Essential: the method autoreleases the
instance, because it returns a pointer to the
created instance
• Not essential: This example uses an existing
initializer, it could use something else or
initialize the Student data directly
11/82
Convenience Constructors
Calling the convenience constructor:
id stud
= [Student studentWithName:@"Johnnie" AndGpa: 3.8];
The message is sent to the Student class object
and returns a pointer to it, the pointer is
assigned to stud
The calling code does neither alloc nor init
An autorelease pool must be in place
12/82
End of MemoryEnd of Memory
• With newer versions of Xcode, the memory
management is becoming more and more
automatic.
• Use Automatic Reference Counting ARC to take
advantage of the latest changes and
improvements with memory management!
13/82
ThankThank You !!!You !!!
For More Information click below link:
Follow Us on:
http://vibranttechnologies.co.in/ios-classes-in-mumbai.html

Ios - Introduction to memory management

  • 1.
  • 2.
  • 3.
    3/82 Objective-C Memory management Memory managementis semi-automatic: The programmer must allocate memory for objects either a) explicitly (alloc) or b) indirectly using a convenience constructor No need to de-allocate
  • 4.
    4/82 Allocation Allocation happens throughthe class method alloc. The message ‘alloc’ is sent to the class of the requested object. Alloc is inherited from NSObject. Every alloc creates a new instance (=object) [HelloWorld alloc]; The class creates the object with all zeros in it and returns a pointer to it. HelloWorld *p = [HelloWorld alloc]; The pointer p now points to the new instance. Now we send messages to the instance through p.
  • 5.
    5/82 The reference counter •Every instance has a reference counter. It counts how many references are retaining the object. The counter is 1 after allocation. It does not count how many references exist to the object • Sending the retain message to the object increases the reference counter by 1. • Sending the release message decreases the reference counter by 1. • No need to do either if using ARC.
  • 6.
    6/82 reference counter =retain counter • When the reference counter reaches zero, the object is automatically de-allocated. The programmer does not de-allocate. • The programmer only does: alloc retain release
  • 7.
    7/82 Rules for memorymanagement • With no ARC: The method that does an alloc or a retain must also do a release, it must maintain the balance between: (alloc or retain) and (release) • If a method does alloc and returns a pointer to the created object then the method must do an autorelease instead of release.
  • 8.
    8/82 Autorelease pool • Withno ARC: For outorelease to work the programmer must create an autorelease pool, using: NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; • When Cocoa is used then the autorelease pool is created automatically – the programmer does not need to do it. • To release the pool and all objects in it, do: [pool release];
  • 9.
    9/82 Convenience Constructors • Thisis a class method that allocates and initializes an object. The programmer is neither doing alloc nor init. • Example: +(id)studentWithName :(NSString*)name AndGpa:(float)gpa { id newInst = [[self alloc]initStudent:name :gpa]; return [newInst autorelease]; }
  • 10.
    10/82 Convenience Constructors • Essential:the method sends alloc to self which is the Student class object • Essential: the method autoreleases the instance, because it returns a pointer to the created instance • Not essential: This example uses an existing initializer, it could use something else or initialize the Student data directly
  • 11.
    11/82 Convenience Constructors Calling theconvenience constructor: id stud = [Student studentWithName:@"Johnnie" AndGpa: 3.8]; The message is sent to the Student class object and returns a pointer to it, the pointer is assigned to stud The calling code does neither alloc nor init An autorelease pool must be in place
  • 12.
    12/82 End of MemoryEndof Memory • With newer versions of Xcode, the memory management is becoming more and more automatic. • Use Automatic Reference Counting ARC to take advantage of the latest changes and improvements with memory management!
  • 13.
    13/82 ThankThank You !!!You!!! For More Information click below link: Follow Us on: http://vibranttechnologies.co.in/ios-classes-in-mumbai.html