Objective-C:MRC手动释放对象内存举例(引用计数器)
手机内存下的类的设计练习:
// Book.h
// 引用计数器
//
// Created by ma c on 15/8/13.
// Copyright (c) 2015年. All rights reserved.
// #import <Foundation/Foundation.h> @interface Book : NSObject
{
NSString *_title;
NSString *_author;
CGFloat _price;
}
-(id)initWithTitle:(NSString*)title andAuthor:(NSString*)author
AndPrice:(CGFloat)price;
-(void)setTitle:(NSString*) title;
-(void)setAuthor:(NSString*) author;
-(void)setPrice:(CGFloat) price;
-(NSString*) title;
-(NSString*) author;
-(CGFloat) price;
-(void) show;
@end
.m声明文件
// Book.m
// 引用计数器
//
// Created by ma c on 15/8/13.
// Copyright (c) 2015年. All rights reserved.
// #import "Book.h" @implementation Book
-(id)initWithTitle:(NSString*)title andAuthor:(NSString*)author
AndPrice:(CGFloat)price
{
self = [super init];
if(self)
{
_title = [title retain];
_author = [author retain];
_price = price;
}
return self;
}
-(void)setTitle:(NSString*) title
{
if(_title != title)
{
[_title release];//释放上一次拥有的对象所有权
_title = [title retain];//获取这一次的对象所有权
}
}
-(void)setAuthor:(NSString*) author
{
if(_author != author)
{
[_author release];//释放上一次拥有的对象所有权
_author = [author retain];//获取这一次的对象所有权
}
}
-(void)setPrice:(CGFloat) price
{
_price = price;
}
-(NSString*) title
{
return _title;
}
-(NSString*) author
{
return _author;
}
-(CGFloat) price
{
return _price;
}
-(void) show
{
NSLog(@"title:%@,author:%@,price:%.2f",_title,_author,_price);
}
-(void)dealloc
{
[_title release];
[_author release];
NSLog(@"title retainCount:0");
NSLog(@"author retainCount:0");
NSLog(@"book retainCount:0");
NSLog(@"book is dealloc!");
[super dealloc];
}
@end
测试Book类
// main.m
// 引用计数器
//
// Created by ma c on 15/8/13.
// Copyright (c) 2015年. All rights reserved.
// #import <Foundation/Foundation.h>
#import "Book.h"
int main(int argc, const char * argv[])
{
//@autoreleasepool { //创建书对象book并初始化
Book *book = [[Book alloc]initWithTitle:@"OC" andAuthor:@"Jobs" AndPrice:35.6];//book count:1
NSLog(@"book retainCount:%lu",[book retainCount]); //创建书名对象title
NSMutableString *title = [NSMutableString stringWithString:@"IOS"];//title count:1
NSLog(@"title retainCount:%lu",[title retainCount]); //设置书名
[book setTitle: title];//title count:2
NSLog(@"title retainCount:%lu",[title retainCount]); //创建书的作者对象author
NSMutableString *author = [NSMutableString stringWithString:@"Bill"];//author count:1
NSLog(@"author retainCount:%lu",[author retainCount]); //设置书的作者名
[book setAuthor:author];//author count:2
NSLog(@"author retainCount:%lu",[author retainCount]); //设置书的价格
[book setPrice:58.9]; //释放title对象所有权----与上面的创建title对象相对应
[title release];//title count:1
NSLog(@"title retainCount:%lu",[title retainCount]); //释放author对象所有权----与上面的创建author对象相对应
[author release];//author count:1
NSLog(@"author retainCount:%lu",[author retainCount]); //释放在book类中的成员实例变量title和author对象的所有权,并销毁book对象
[book show];
[book release];//title count:0, author count:0 ,book count:0, dealloc book
//}
return ;
}
运行结果:
-- ::49.608 引用计数器[:] book retainCount:
-- ::49.609 引用计数器[:] title retainCount:
-- ::49.610 引用计数器[:] title retainCount:
-- ::49.610 引用计数器[:] author retainCount:
-- ::49.610 引用计数器[:] author retainCount:
-- ::49.610 引用计数器[:] title retainCount:
-- ::49.610 引用计数器[:] author retainCount:
-- ::49.610 引用计数器[:] title:IOS,author:Bill,price:58.90
-- ::49.611 引用计数器[:] title retainCount:
-- ::49.611 引用计数器[:] author retainCount:
-- ::49.611 引用计数器[:] book retainCount:
-- ::49.611 引用计数器[:] book is dealloc!
Program ended with exit code:
可以看出:
Objective-C:MRC手动释放对象内存举例(引用计数器)的更多相关文章
- UNITY Destroy()和DestroyImadiate()都不会立即释放对象内存
如题,destroyimadiate是立即将物体从场景hierachy中移除,并标记为 "null",注意 是带引号的null.这是UNITY内部的一个处理技巧.关于这个技巧有很争 ...
- 手动释放linux内存cache
总有很多朋友对于Linux的内存管理有疑问,之前一篇linux下的内存管理方式似乎也没能清除大家的疑虑.而在新版核心中,似乎对这个问题提供了新的解决方法,特转出来给大家参考一下.最后,还附上我对这方法 ...
- Objective-C:ARC自动释放对象内存
ARC是cocoa系统帮你完成对象内存释放的引用计数机制 .h文件 // Person.h // 01-ARC // // Created by ma c on 15/8/13. // Copyrig ...
- linux如何手动释放linux内存
当在Linux下频繁存取文件后,物理内存会很快被用光,当程序结束后,内存不会被正常释放,而是一直作为caching.这个问题,貌似有不少人在问,不过都没有看到有什么很好解决的办法.那么我来谈谈这个问题 ...
- Java手动释放对象
伪代码 public void updateUser(BufferedWriter writer, BufferedReader reader) { List<User> array = ...
- 手动释放Linux内存
查看内存: [root@iZ9dp52tlpqyihuisujjswZ bin]# free -h total used free shared buff/cache available Mem: 3 ...
- 可用内存free不足 hadoop3 无法启动 手动释放缓存 cache
[root@hadoop3 hadoop]# xlfg total used free shared buff/cache availableMem: 15 0 2 0 12 14Swap: 7 0 ...
- 【cocos2d-x 3.x 学习笔记】对象内存管理
内存管理 内存管理一直是一个不易处理的问题.开发人员必须考虑分配回收的方式和时机,针对堆和栈做不同的优化处理,等等.内存管理的核心是动态分配的对象必须保证在使用完成后有效地释放内存,即管理对象的生命周 ...
- oc48--多个对象内存管理练习
// // main.m // 多个对象内存管理练习 // // ARC是Xcode帮我们生成内存释放的代码,MRC是需要我买自己写retain和release.想研究内存管理只能在MRC,管理对象就 ...
随机推荐
- 最短路算法 -- SPFA模板
一.算法步骤 建立一个队列,初始时队列里只有起始点,再建立一个数组记录起始点到所有点的最短路径(该数组的初始值要赋为极大值,该点到它本身的路径赋为0,下面的模板中该数组为dist[]).然后执行松弛操 ...
- forkcms 开启调试模式
You can enable debug mode by adding "SetEnv FORK_DEBUG 1" in your virtualhosts file.
- 20169211《Linux内核原理与分析》第六周作业
1.教材内容总结 2.实验报告 3.学习总结 一.教材内容总结 1.系统调用与应用编程接口API的区别 操作系统为用户态进程与硬件设备进行交互提供了一组接口,就是系统调用.它主要有一下三个方面的作用: ...
- Python 中的函数
学了 Python 中的数据类型,语句,接下来就来说一下 Python 中的函数,函数是结构化编程的核心.我们使用函数可以增加程序的可读性.自定义函数时使用关键字def 函数由多条语句组成.在定义函数 ...
- <泛> STL - vector 模拟实现
今天为大家带来一个模拟STL-vector的模板实现代码. 首先看一下测试结果,之后再为大家呈现设计 测试效果 测试代码 #include<iostream> #include<ve ...
- mysql高性能索引
独立索引: 独立索引是指索引列不能是表达式的一部分,也不能是函数的参数 例1: SELECT actor_id FROM actor WHERE actor_id+1=5 --这种写法,就算在acto ...
- 使用Python发送HTML格式的邮件(收到的邮件有发送方才是正解)
发送html格式的和普通文本格式差不多,只是MIMEText(content,"html","utf-8"))与MIMEText(content,"p ...
- Codeforces Round #222 (Div. 1) D. Developing Game 扫描线
D. Developing Game 题目连接: http://www.codeforces.com/contest/377/problem/D Description Pavel is going ...
- .net正则提取手机号码,并替换带有手机号码的a标签
//用正则查找字符串中的手机号码,最后替换成带a标签的可打电话的字符串 var str = "收件人:江苏省苏州市工业园区屌丝大叔收,电话:18688888888"; var re ...
- spring---aop(9)---Spring AOP中引入增强
写在前面 Spring将introduction通知看作一种特殊类型的拦截通知.用Spring的行话来讲,对方法的增强叫做Wearing(织入),而对类的增强叫introduction(引入).Int ...