【ObjC那点儿事儿】OC对象内存管理 MRR

来源:互联网 发布:打印机数据线速度 编辑:程序博客网 时间:2024/05/01 13:27

// 系统版本:OS X 10.9.2 (13C64)

// Xcode版本:5.0.2 (5A3005)


Objective-C的对象生成之后保存在堆内存中,用一个指针来指向它。对象在使用完成之后不会自动销毁,如果不进行管理的话,会产生内存泄露。

OC内存管理基于对象占用。一个对象可能被一个或多个对象占用。只要对象被至少一个对象占用,该对象就继续存在。如果改对象不再被占用,系统会自动销毁改对象,释放对象占用的内存空间。
OC用引用计数(retain count)来实现对象占用。每个对象都有一个引用计数器,表示对象被引用的次数。
1、你拥有自己创建的任何对象。 可以使用任何 “alloc”, “new”, “copy”, 或者 “mutableCopy” 开头的方法(例如:alloc)创建对象,对象的retain count =1
2、可以使用 retain 占用一个对象,对象收到 retain 消息,retain count +1
3、当拥有的某个对象不再使用时,必须解除占用,给对象发送 release 消息,对象的 retain count -1,
或者给对象发送 autorelease 消息,自动释放池会在结束时,对象的 retain count -1 ,当 retain count 归 0 的时候,对象就被真正释放了。

4、不能解除占用不属于自己的对象,也就是说,不是自己创建的对象,就不能解除占用

下面引用一个Xcode Document关于内存管理管理的图片:



Basic Memory Management Rules

The memory management model is based on object ownership. Any object may have one or more owners. As long as an object has at least one owner, it continues to exist. 
If an object has no owners, the runtime system destroys it automatically. To make sure it is clear when you own an object and when you do not, Cocoa sets the following policy:

<1> You own any object you create
    You create an object using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or mutableCopy).

<2> You can take ownership of an object using retain
    A received object is normally guaranteed to remain valid within the method it was received in, and that method may also safely return the object to its invoker. 
    You use retain in two situations: 
        (1) In the implementation of an accessor method or an init method, to take ownership of an object you want to store as a property value; 
        (2) To prevent an object from being invalidated as a side-effect of some other operation (as explained in “Avoid Causing Deallocation of Objects You’re Using”).


<3> When you no longer need it, you must relinquish ownership of an object you own
    You relinquish ownership of an object by sending it a release message or an autorelease message. 
    In Cocoa terminology, relinquishing ownership of an object is therefore typically referred to as “releasing” an object.

<4> You must not relinquish ownership of an object you do not own
    This is just corollary of the previous policy rules, stated explicitly.

The ownership policy is implemented through reference counting—typically called “retain count” after the retain method. Each object has a retain count.

<1> When you create an object, it has a retain count of 1.
<2> When you send an object a retain message, its retain count is incremented by 1.
<3> When you send an object a release message, its retain count is decremented by 1.
<4> When you send an object a autorelease message, its retain count is decremented by 1 at the end of the current autorelease pool block.

If an object’s retain count is reduced to zero, it is deallocated.

No 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed
Default property attribute 'assign' not appropriate for non-GC object
0 0
原创粉丝点击