Memory Management in iOS

Source: Internet
Author: User

in iOS steps to create an object:

1, allocating memory space, storing objects
2, initialize member variables
3, returns the pointer address of the object

First: Non-arc mechanism:

1, the object is created at the same time, the internal will automatically create a reference counter, is the system used to determine whether to reclaim the object's unique indicator, when our application count Retaincount = 0, the system will recycle the current object
2,[object Release] retainCount-1;
3,[Object Retain] Retailcount + 1;
4, when the application count Retailcount = 0 object will be destroyed;

5,dealloc function, when an object is destroyed, the system will automatically call the Dealloc function, notifying the object will be destroyed

Memory management principles (pairing principle): As long as there is new,alloc,retaim, there must be a pairing release,autorelease

  Note: exc_bad_access access to inaccessible memory space
Objects that are reclaimed by the system, called Zombie objects
By default, Xcode does not always check for zombie objects in order to improve encoding efficiency

Manual memory research issues include:
1, Wild pointer operation
2, Memory leaks: objects that are no longer being used are not destroyed in memory


Interview written test, the rate is very high

- (void) Setcar: (Car *) car{if(_car! =car) { //Relese Old value[_car release];//[Nil release]; //retain new value_car =[car retain]; }}-(Car *) car{return_car;}- (void) dealloc{//The goal is to ensure that when the P object exists, the car object must exist    [_car release];  [Super Dealloc]; NSLog (@"the person was destroyed.");}

Second: ARC mechanism

To study the arc mechanism, it is necessary to understand the role of @property:

1. Declaration of production get and set methods
2. Generate a simple implementation of get and set methods
3. If you do not have a member variable of the Fame object, then he will automatically generate a member variable at the beginning of _

@property parameters are divided into 4 categories

1. Parameters related to the Set method memory management
Retain: To generate a set method that conforms to the memory management principles (Application and object type)
Assign: Direct assignment, (object type, basic data type), simple
Copy:


2. Multithreading-related

Nonatomic: Do not generate multithreaded line pipe code, use this can be (more efficient)
Atomic: Generate multithreaded Line Pipe code (this is not the default way to write)
In the actual development, as long as the object type of the @property are written below

3. Do you want to generate the set and get methods
ReadWrite: Readable writable property, simultaneous generation of set and get methods
ReadOnly: Read-only property, generating only get methods

4.set parameters associated with the Get method name
Setter: Sets the generated Set method name
Getter: Set the generated Get method name

For example:

@property (nonatomic,retain) car * car;

@property (nonatomic,retain) nsstring * name;
@property (nonatomic,assign) int age; int float Double Char
@property (nonatomic,assign) int weight;
@property (nonatomic,assign,readonly) int idcard;
Changing set and get method names is a lot of variables that are used in type bool
@property (nonatomic,assign,setter = Isdeid:,getter = Isdeid) BOOL Isdeid;

Memory Management in iOS

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.