`
JavaZhuang
  • 浏览: 9587 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

4 3.OC9-内存管理1-retain和release

    博客分类:
  • IOS
 
阅读更多

4 3.OC9-内存管理1-retainrelease

 

内存管理

范围:

任何继承了NSObject的对象,对基本数据类型无效。

 

原理:

1)、每个对象内部保存了一个与之相关联的整数,称为引用计数器

2)、当使用allocnew或者copy创建一个对象时,对象的引用计数器设置为1

3)、给对象发送一条retain消息,可以使引用计数器值+1

4)、给对象发送一条release消息,可以使引用计数器值-1

5)、当一个对象的引用计数器值为0时,那么它将被销毁,其占用的内存被系统回收,OC也会自动向对象发送一条dealloc消息。一般会重写dealloc方法,在这里释放相关资源。一定不要直接调用dealloc方法。

6)、可以给对象发送retainCount消息获得当前的引用计数器值。

 

 

内存管理原则

1)、谁创建,谁释放(“谁污染,谁治理”)。如果你通过allocnew或(mutablecopy来创建一个对象,那么你必须调用release autorelease。换句话说,不是你创建的,就不用你去释放。

2)、一般来说,除了allocnewcopy之外的方法创建的对象都被声明了autorelease

3)、谁retain,谁release。只要你调用了retain,无论这个对象是如何生成的,你都要调用release

 

例一:

main.m

 //

//  main.m

//  内存管理retianrelease

//

//  Created by qwz on 13-12-9.

//  Copyright (c) 2013 renhe. All rights reserved.

//

 

#import <Foundation/Foundation.h>

#import "Student.h"

 

 

int main(int argc, const char * argv[])

{

 

    @autoreleasepool {

        Student *stu = [[Student alloc] init]; //1

        

        [stu release]; //0

        

        //[stu release]; //会发生野指针错误,也就是说访问了不属于你的内存

    }

    return 0;

}

 

Student.h

 //

//  Student.h

//  内存管理retianrelease

//

//  Created by qwz on 13-12-9.

//  Copyright (c) 2013 renhe. All rights reserved.

//

 

#import <Foundation/Foundation.h>

 

@interface Student : NSObject

 

@propertyint age;

 

@end

 

Student.m

 //

//  Student.m

//  内存管理retianrelease

//

//  Created by qwz on 13-12-9.

//  Copyright (c) 2013 renhe. All rights reserved.

//

 

#import "Student.h"

 

@implementation Student

@synthesize age = _age; //xcode5.0环境下是可以省略的

 

- (void) dealloc{

    NSLog(@"%@被销毁了", self);

    

    [superdealloc]; //一定要调用superdealloc方法,而且最好要放在最后面调用

    

}

@end

 

  运行结果

2013-12-09 11:32:58.451 内存管理retianrelease[466:403] <Student: 0x1001144d0>被销毁了

 

 

----------------

例二:

main.m

 //

//  main.m

//  内存管理retianrelease

//

//  Created by qwz on 13-12-9.

//  Copyright (c) 2013 renhe. All rights reserved.

//

 

#import <Foundation/Foundation.h>

#import "Student.h"

 

 

int main(int argc, const char * argv[])

{

 

    @autoreleasepool {

        Student *stu = [[Student alloc] init]; //1

        

        //z代表无符号

        NSLog(@"count:%zi", [stu retainCount]);

        

        [stu retain]; //2

        

        NSLog(@"count:%zi", [stu retainCount]);

        

        [stu release]; //1

        

        NSLog(@"count:%zi", [stu retainCount]);

        

        [stu release]; //0

        

        //[stu release]; //会发生野指针错误,也就是说访问了不属于你的内存

    }

    return 0;

}

 

Student.h

 //

//  Student.h

//  内存管理retianrelease

//

//  Created by qwz on 13-12-9.

//  Copyright (c) 2013 renhe. All rights reserved.

//

 

#import <Foundation/Foundation.h>

 

@interface Student : NSObject

 

@propertyint age;

 

@end

 

Student.m

 //

//  Student.m

//  内存管理retianrelease

//

//  Created by qwz on 13-12-9.

//  Copyright (c) 2013 renhe. All rights reserved.

//

 

#import "Student.h"

 

@implementation Student

@synthesize age = _age; //xcode5.0环境下是可以省略的

 

- (void) dealloc{

    NSLog(@"%@被销毁了", self);

    

    [superdealloc]; //一定要调用superdealloc方法,而且最好要放在最后面调用

    

}

@end

 

 运行结果

2013-12-09 10:59:09.977 内存管理retianrelease[447:403] count:1

2013-12-09 10:59:09.987 内存管理retianrelease[447:403] count:2

2013-12-09 10:59:10.002 内存管理retianrelease[447:403] count:1

2013-12-09 10:59:10.012 内存管理retianrelease[447:403] <Student: 0x1001144d0>被销毁了

 

分享到:
评论

相关推荐

    千锋OC源码

    OC语言教程-第4讲-内存管理2-retain点语法 OC语言教程-第5讲-内存管理3-MyArray OC语言教程-第6讲-内存管理4-AutoreleasePool OC语言教程-第7讲-协议 OC语言教程-第8讲-协议代理设计模式 OC语言教程-第9讲-Category...

    kafka集群监控之KafkaOffsetMonitor 0.4.6版本地化(实测)

    --kafkaBrokers ip1:ports,ip2:ports,ip3:ports \ --zk ip1:ports,ip2:ports,ip3:ports \ --port 8181 \ --refresh 10.seconds \ --retain 5.days \ --dbName offsetapp_kafka 1&gt;out.log 2&gt;err.log & stop...

    更新上个angular版本问题 Kafka监控工具KafkaOffsetMonitor-assembly-0.4.6-SNAPSHOT.jar

    KafkaOffsetMonitor-assembly-0.4.6-SNAPSHOT.jar ... --retain 7.days 1&gt;/home/bigdata/kafkamonitor/stdout.log 2&gt;/home/bigdata/kafkamonitor/stderr.log \ --dbName offsetapp_kafka & echo "OK.....

    CompTIA.Linux.LPIC-1.Cert.Guide.07897545

    The LPIC-1 Authorized Cert Guide has a single goal: to help you pass the new version of the Linux Professional Institute LPIC-1 exams. The most comprehensive and time-efficient LPIC-1 study guide ...

    二、IOC容器bean的实例化.md

    cp KafkaOffsetMonitor-assembly-0.2.0.jar com.quantifind.kafka.offsetapp.OffsetGetterWeb --port 8088 --zk 192.168.164.110:2181,192.168.164.120:2181,192.168.164.130:2181 --refresh 10.seconds --retain 1....

    KafkaOffsetMonitor-assembly-0.2.1.jar 监控kafka,已手动替换angular js,绝对可用

    自己手动替换js,且下载了:angular-...java -cp KafkaOffsetMonitor-assembly-0.2.1.jar com.quantifind.kafka.offsetapp.OffsetGetterWeb --zk 192.168.168.**:2181 --port 8088 --refresh 5.seconds --retain 1.days

    对于Retain和Assign属性的理解

    对于Retain和Assign属性的理解

    Objective-C内存管理课件.doc

    Objective-C内存管理课件.docx ,assign,retain,copy

    An Introduction to Elm

    Maybe it will be a paper book someday, but I plan to retain ownership of the material such that the complete book is always available for free online. ------------------------------------------------...

    KafkaOffsetMonitor-0.2.1

    简单的启动: java -cp KafkaOffsetMonitor-assembly-0.2.1.jar \ ... --zk dn1:2181,dn2:2181,dn3:2181 \ --port 8089 \ --refresh 10.seconds \ --retain 1.days 详细使用,请自觉百度。

    KafkaOffsetMonitor-assembly-0.2.0

    亲测可用哦 java -jar KafkaOffsetMonitor-assembly-0.2.0.jar \ com.quantifind.kafka.offsetapp.OffsetGetterWeb \ --zk xxx \ --port 8086 \ --refresh 10.seconds \ --retain 7.days &

    httpd2.2.12.tar

    Release Manager will generally roll a release candidate (APACHE_#_#_#_RC#) tag. Release Candidate tarballs will be announced to the stable-testers@httpd.apache.org for the stable tree. Then, the ...

    飞鸽传书Ver2.06源码

    9. 更新历史 10. 感谢 ================================================================================ 重要说明: comctl32.dll(公共控件) 要求 4.71 或以上版本 更多信息请参见 "系统要求" =======...

    Fast copy(快速拷贝)

    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above ...

    iOS 面试题

    1.关键字 retain 和 release 的 功能 retain 是对oc对象计数器+1 release是对oc对象计数器-1 减到0就自动调用oc对象的dealloc函数 2.请问关键字 alloc 和 init 的 区别 alloc是分配内存,对象计数器为1 init是...

    apktool documentation

    Every Apktool release contains internally the most up to date AOSP framework at the time of the release. This allows you to decode and build most apks without a problem. However, manufacturers add ...

    Retain.pdf

    SAS-The RETAIN Statement; Retain的用法,英文版。

    Fastcopy2.08(x32)

    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above ...

    retain和copy还有assign的区别

    retain和copy还有assign的区别

Global site tag (gtag.js) - Google Analytics