`

IOS内存管理tips

 
阅读更多
IOS中的内存管理是通过reference count来管理的,

IOS框架提供了2种内存管理方法,MRR(manual retain-release)和ARC(automatic retain-release)


1、Release或者Overwriting还在使用的数据回导致程序崩溃
2、不Release不再使用的数据导致内存泄露,会影响系统性能并导致程序异常退出。


Memory Management Policy:


“alloc”,“new”,“copy”,“mutableCopy” 创建一个新对象


“retain”关键字使用场景:(1)保存一个属性值,(2)防止自己需要的对象被释放


“release”,“autorelease" 释放一个对象。
”autorelease“ 含义就是我想释放一个对象,但是允许调用方法的对象使用该对象之后再释放。即其生命延长到调用该对象的方法的生命周期上了。


Example:

{

    Person *aPerson = [[Person alloc] init];


    // ...


    NSString *name = aPerson.fullName;


    // ...


    [aPerson release];


}


无需释放name,因为不是我们创建的,不归我们释放,只释放自己创建的对象。


”dealloc“ 不要直接调用其他对象的dealloc,实现完自己的dealloc逻辑之后,一定要调用父类的dealloc。


Practical Memory Management:
1、关于对象属性的内存管理,一般情况下,直接使用Synthesize关键字就行了,它会自动帮助我们生成setter和getter方法,让生命变简单一点,。
2、使用setter方法给属性赋值
3、不要在initialize,dealloc方法中使用Accessor方法,可以直接给属性赋值。

内存检测工具
1、Analyze
2、Instrument


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics