Objective-C learner _ memory management learning notes, objectivec Memory Management

Source: Internet
Author: User

Objective-C learner _ memory management learning notes, objectivec Memory Management

 

Memory leakage: the discarded objects continue to exist after the lifecycle is exceeded.

Memory Management: adhere to the pairing principle. When alloc/new/copy/mutableCopy is available, the corresponding release or autorelease is required.

Cause: the Oc object is in the heap (Dynamic Allocation), and the memory in the heap is managed by our programmers;

 

Object operations

Objective-c method

Generate and hold objects

Alloc/new/copy/mutableCopy

Owner

Retain Method

Release object

Release Method

Deprecated object

Dealloc method (automatically called by the System)

Memory Management (in this article, "yourself" indicates "use environment of objects" or "developers "):

 

1. Self-generated object, owned

2. You can own non-self-generated objects.

3. release an object that you no longer need

4. objects not owned by the user cannot be released

 

Summary:

1. After calling the alloc or retain method, the reference count value is + 1

2. After the release method is called, the reference count value is-1.

3. When the reference count value is 0, call the dealloc method to discard the object.

4. Call retianCount to return the number of counter references.

 

Multi-object memory management steps: (use @ property to generate the following set method)

1. Call the passed parameter retain method in the set method.

-(Void) setRoom (Room *) room

{

If (_ room! = Room) {// determines whether the passed object is the current object.

[_ Room relase]; // release the current object

_ Room = [room retain]; // reference a new object and assign a value

}

2. Call release in the dealloc Method

-(Void) dealloc

{

[_ Room release];

_ Room = nil;

[Super dealloc];

}

 

Autorelease usage:

 

# Use of import and @ class:

# Import: contains all the information, including the variables and methods of the referenced class.

@ Class: A class can be referenced simply (just tell the compiler that this is a class)

The main difference between @ class and # import: # copy the imported content once it changes.

How to use:

(1) Use @ class in. h file

(2) # import is used in the. M file. # import is required only when it is actually used.

(3) # import "B", # import "A" in a.h, resulting in an error (Unknown type name ......)

 

Retain: retain is used at one end, and assign is used at the other end.

 

Common knowledge points or notes:

(1) The counter for creating an object is 1.

(2) When rewriting the deallloc method: [super dealloc] must be placed at the end.

(3) If an object is released, assign a value to the address to nil to prevent the operation of the wild pointer.

(4) Sending a message to a null pointer will not cause errors.

(5) Calling the retain method will return the object itself

(6) The pointer assignment does not call retain. You need to manually call retain.

(7) @ property: the set method generated has implemented the above set method and get method. assign is the default method.

(8) Obtain an object generated by yourself and held by yourself: id obj = [NSMutableArray array]; holding object: [obj retain]; (autorelease is automatically called in the class method)

(9) If you use an object that is not owned by yourself, the program will crash.

(10) _ weak (which can only be used for ios5 or above): circular reference can be avoided, and the variable __modified does not hold the object.

(11) do not place large memory usage in autoreleasepool, which may cause performance problems.

(12) You must use self when rewriting the alloc method, so that the subclass can also create the corresponding object.

(13) instancetype, more intelligent than id, can be found during compilation

(14) If _ weak UIView * view = [[UIView alloc] init] is used directly, a warning will be issued: Assigning retained object to weak variable ......

(15) advantage of weak application: When holding a weak reference of an object, if the object is discarded, the weak reference will automatically expire and be in the nil (assigned State) safer than _ unsafe_unretained

(16) Non-OC objects are placed in the stack, and pointer variables (4 bytes) exist in the stack.

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.