OC Memory Management

Source: Internet
Author: User

Scope of administration: Any object that inherits NSObject, is not valid for other basic data types.

When the code is near the end, all local variables in the code block are recycled, and pointers to the objects are recycled, when the object

There is no pointer pointing at him, but it still exists in memory, causing a memory leak.

Classification of--------------------------------memory leaks--------------------------------

Often: The code that occurs in memory leaks is executed multiple times, and each time it is executed, it causes a memory leak.

Sporadic: occurs only under certain circumstances or operating procedures. Frequent and incidental is relative, for a particular environment, the occasional is also become frequent.

One-time: A memory leak occurs when the code is executed only once, or because of an algorithm flaw, there is always a memory leak.

Implicit: Generally speaking (memory is released after the program ends, but some programs open for one months, causing a memory leak to accumulate, eventually draining the system's memory).

------------------------------the basic structure and reference count of the object------------------------

Each object in OC is counted by its own reference, an integer that indicates the number of times the object has been referenced, or how many things are being used in this object, the default counter value is 1 when the object has just been created, but the value of the counter is 0 o'clock and the object is destroyed.

The only way to determine whether an object should be recycled is if the counter is 0, or if not 0.

-------------------------------Operation--------------------------------------

Sends a message to the object for the corresponding counter operation.

Retain message: Make counter +1, change method return object itself

Release message: Make Counter-1, (does not represent releasing the object itself)

Retaincount: Gets the object's current reference counter value

Destruction of------------------------------------Objects--------------------------------------------

When an object has a reference count of 0 o'clock, the object is destroyed and its occupied memory space is reclaimed.

* When the object is destroyed, the system automatically sends an DEALLOC message to the object, typically overriding the Dealloc method, which releases the associated resources in such a way that dealloc is like the object's last words. Once the Dealloc method is overridden, "super dealloc" must be called and placed in the last call of the code block (the Dealloc method cannot be called directly).

Note: When an object is reclaimed and its memory is reclaimed, it cannot access the memory space by the pointer (wild pointer)

-------------------------------Zombie Object----------------

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

Null pointer: There is no pointer to anything (something stored is 0,null,nil), and sending a message to a null pointer does not give an error.

Note that you cannot use [instance object retain] to bring an informed object back to life.

Principles of memory management

1. Principles

As long as someone else is using an object, the object will not be recycled.

As long as you want to use this object, you should let the object reference counter +1.

When you do not want to use this object, you should let the object reference counter-1.

2, who created, who release

(1), if you create an object through Alloc, new, copy, then you must call release or Autorelease

(2), you do not create it will not be responsible for you.

3, who retain, who release

As long as you call the retain, no matter how this object is generated, you have to call release

4. Summary

Beginning and end, there is a plus minus, if you let an object counter plus 1, it should be behind it-1.

Memory Management Code Specification

-(void) Setcar: (Car *) car

{

1. First determine whether the new incoming object

If (Car!=_car)

{

2 Make a release to the old object once

[_car release];//If there is no old object, it has no effect

3. Do a retain on a new object

_car=[car retain];

}

}

Code specification for the Dealloc method

(1) Be sure to [Super Dealloc], and put it to the last

(2) Do a release operation on other objects owned by self (current)

-(void) dealloc

{

[_car release];

[Super Dealloc];

}

Autorelease

1. Basic usage:

A, the object is placed in an auto-release pool

b, when the automatic release pool is destroyed, all objects in the pool will be released once

C, the object itself is returned

D, after the Autrelease method is called, the object's counter is unaffected (destroyed)

2. Benefits

A, do not need to be released at the time of the relationship object

b, no need to relationship when call release

3. Use

A, occupy large memory objects, do not casually shiyongautrelease, should use release to precisely control

B. Objects with less memory use autrelease, with no significant impact.

4.

Multiple use of Autrelease

Alloc is called after the Autrele, and then the release is called

5, automatic release of the pool

A, during the operation of the iOS program, will create countless pools, these pools are in the stack structure (advanced) exist

B, when an object calls Autrele, the object is placed in the release pool at the top of the stack

OC Memory Management

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.