OBJECTIVE-C (Memory management)

Source: Internet
Author: User

Reference counter

Each OC object has a reference counter that occupies 4 bytes of storage space

When you use or create an object, the reference counter for the new object defaults to 1

Retain: Can make reference counter +1

Release: Can be a reference counter-1

Retaincount: Gets the value of the current reference counter

The Dealloc method is overridden when the object is destroyed

-(void) dealloc

{

This sentence must be put on the last side.

[Super Dealloc];

}

Zombie objects: Objects that occupy memory have been reclaimed, zombie objects can no longer be used

Wild pointer: Pointer to zombie object (memory not available)

Error: Exc_bad_access: Access to a piece of bad memory (has been recycled, unusable memory)

Null pointer: No pointer to anything (save nil, NULL, 0), send message to null pointer without error

Multi-Object Memory management

1. When using (Occupy) an object, you should let the object's reference counter +1

(Let the object do one retain operation)

2. When you do not use (occupy) an object, you should let the object's reference counter-1

(Let the object do a release operation)

3. Who retain, who release

Memory management of the Set method

Memory Management Code Specification:

1. If you call Alloc, you must have release (Autorelease), the object is not created with alloc, do not write release

Code specification for 2.set methods

1 "Basic Data type: Direct assignment

-(void) Setage: (int) Age

{

_age = age;

}

2> OC Object Type

-(void) Setcar: (Car *) car

{

1. First to determine whether the new object is passed in

if (car! = _car)

{

2. Do a release of the old object once

[_car release];

3. Do a retain on a new object

_car = [car retain];

}

}

Code specification for 3.dealloc methods

1> must be [Super Dealloc], and put it on the last side.

2> do a release for other objects owned by self (current)

-(void) dealloc

{

[_car release];

[Super Dealloc];

}

1.set method 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

2. Whether to generate a set method

* ReadWrite: Generate both setter and Getter Declaration, implementation (default)

* ReadOnly: Only generate getter declarations, implementations

3. Multithreading Management

* nonatomic : High performance (usually with this)

* Atomic: Low performance (default)

The name of the 4.setter and Getter 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)

@property parameters

OC Object Type:

@property (nonatomic, retain) class name * attribute name;

@property (nonatomic, retain) Car *car;

@property (nonatomic, retain) ID *car;

Non-OC Object types

@property (nonatomic, assign) type name attribute name;

@property (nonatomic, assign) int age;

@class person;

@class role: Tell the compiler that person is a class

Specification for referencing a class in development

1. Declare the class with @class in the. h file

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

Both ends of the circular reference solution:

1 "one end with retain

2 "one End with Assigh

Autorelease

During the operation of the iOS program, countless pools are created with the presence of a stay structure (advanced post-exit)

When an object calls the Autorelease method, the object is placed in the free pool at the top of the

1. The Autorelease method returns the object itself

2. Autorelease will put the object in an auto-release pool

3. When the auto-release pool is destroyed, a release operation is made for all objects in the pool

IOS 5.0 starts by creating a release pool as follows:

@autoreleasepool

{//{curly braces are created at the beginning to automatically free the pool

}//} Curly brace end represents destruction of the release pool

Autorelease Use Note:

1. Do not use autorelease for objects with large memory

2. Objects with less memory use autorelease, not much impact

The system comes with a method that does not contain alloc, new, copy, indicating that the returned objects are autorelease

Nsstring:ns is the prefix, Next Step

Some class methods are often provided in development to quickly create an already autorelease object

Cases:

+ (ID) person

{

Do not use the class name directly when creating the object, usually with self

return [[[Self alloc] init] autorelease];

}

ARC

Automatic Reference Counting

ARC's judgment: The object is freed as long as no strong pointer is pointed to the object

1.ARC Features

1> does not allow calls to release, retain, Retaincount

2> allows rewriting of dealloc, but does not allow calls to [super Dealloc]

Parameters of the 3> @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

4> former retain changed to strong

The hands are divided into 2 types:

1> Strong pointer: By default, all pointers are strong pointers __strong

2> Weak pointer: __weak

Wrong wording (meaningless notation):

__weak person *p = [[Person alloc] init];

When both ends are circular references, the solution:

1> ARC

1 End With strong, another 1 end with weak

2> Non-Arc

1 End with retain, another 1 end with assign

OBJECTIVE-C (Memory management)

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.