OBJECTIVE-C Memory Management and ARC

Source: Internet
Author: User

Memory management

Range : Any object that inherits NSObject is not valid for the base data type

principle: an integer associated with it is stored inside each object called the reference counter

1. Basic operation of the counter

When you create an object using Alloc new or copy, the object's reference counter is set to 1

Retain: Counter +1 returns the object itself

Release: Counter-1 No return value

Retaincount: Gets the current counter value

Dealloc: When an object is to be recycled, it is called to call [Super Dealloc]; This call should be placed on the last side .

Zombie Object: The object that is occupied by the memory has been reclaimed zombie objects can no longer be used

Wild Pointer: A pointer to a zombie object (memory not available) sends a message to the Wild Pointer error (exc_bad_access)

NULL pointer: There is no pointer to anything (the stored thing is nil NULL 0) sending a message to a null pointer does not give an error

2. Memory Management Principles

Who created who is released if you create an object by Alloc new or copy then you must call release or autorelease in other words you don't create it, you don't have to release it.

Generally, objects created in addition to the alloc new or copy methods are declared Autorelease

Who retain who release as long as you call retain no matter how this object is generated you have to call release

Specification: as long as the call to Alloc must have release or Autorelease

Implementation of Set method

/** Basic Data type Direct copy **/- (void) Setage: (int) Age {_age=Age ;}/** OC Object type **/- (void) Setcar: (Car *) Car {//decide if it's a new incoming object.    if(Car! =_car) {        //do a release on the old object once[_car release]; //do a retain on a new object_car =[car retain]; }}

Implementation of Dealloc method

// be sure to [Super Dealloc]; and put it on the last side. // Do a release-(void) dealloc {[_car release] for the object owned by self (current)    ;    [Super Dealloc];}

[Email protected] Parameters

@property (parameters, parameters ...) int height;

Note that parameters of the same type cannot be written more than one

A.set methods Memory management-related parameters

Retain:release old value retain new value (for OC object type)

Assign: Direct assignment (default for non-OC object types)

Copy:release old value Copy new value

B. Whether to generate a set method

ReadWrite: Simultaneous generation of set and get declaration implementations (default)

ReadOnly: Only a Get declaration implementation is generated

C. Multi-threaded management

Nonatomic: High performance (usually with this)

Atomic: Low performance (default)

Name of the D.set and Get methods

Setter =: Determines the name of the set method and must have a colon:

Getter =: Determines the name of the Get method (typically used in bool type)

4. Cyclic retain and @class

Cyclic retain

For example a object retain a B object B object retain a object

This causes the A object and the B object to never be freed

Solution: When the ends are referenced to each other, one end should be used with the retain end assign

@class role: Just tell the compiler that a name is a class

@class person; Just tell the compiler that the person is a class

Referencing a specification of a class in development

Declaring a class with @class in an. h file

Use #import in. m files to contain everything in the class

5.autorelease

intMain () {@autoreleasepool {//{Start represents the release pool createdPerson *p =[[Person alloc] init] autorelease]; P.age=Ten; }//the end represents the destruction of the release pool    return 0;}/** Autorelease Basic usage **///The object is placed in an auto-release pool//when the auto-free pool is destroyed, a release operation is made for all objects in the pool//The object itself is returned//the object's counter value does not change after the Autorelease method is called/** Benefits of Autorelease **///No more worrying about the time the object was released.//No more worrying about when to call release/** Autorelease Use note **///objects that occupy large memory do not use autorelease casually//objects that consume less memory use autorelease do not have much effect/** Wrong wording **///Alloc called Autorelease and release.//successive calls to multiple autorelease/** Automatic release of the pool **///countless pools are created while the iOS program is running these pools are present in a stack structure (advanced post-exit)//when an object calls the Autorelease method, it puts the object at the top of the heap./** The development process often write some class methods to quickly create a Autorelease object when creating objects do not use the class name directly with self **/

ARC

1. Basic Introduction

ARC is a new feature added since iOS 5 completely eliminates the cumbersome compiler that manually manages memory automatically inserts the appropriate retain release autorelease statement in place you no longer need to worry about memory management because the compiler handled everything for you.

ARC is a compiler feature and not an iOS runtime feature unlike the garbage collector in other languages so arc and manual memory management performance is the same sometimes even faster because the compiler can also perform some optimizations

2. Fundamentals

Rule: The rules of Arc are very simple as long as there is a strong pointer variable pointing to the Object object will remain in memory

Strong pointers: By default all pointers are strong pointers __strong

Weak pointer: A weak pointer to an object that is reclaimed after the weak pointer automatically becomes a nil pointer does not throw a wild pointer error __weak

3. Use note

Cannot call release, retain, Autorelease, Retaincount

can override Dealloc, but cannot call [super Dealloc];

Parameters for @property

Strong: Member variable is strong pointer (for OC object type)

Weak: member variable is weak pointer (for OC object type)

Assign: For non-OC object types

Replace retain with strong when arc is not used

When both ends are referenced to each other: one end is used with strong weak

Turn off arc for the entire project

Turn off arc for a. m file

OBJECTIVE-C Memory Management and ARC

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.