Objective-c语言_内存管理2(代码)
来源:互联网 发布:mac pro截屏快捷键 编辑:程序博客网 时间:2024/06/07 10:47
在XCode4.2以上的版本为ARC(自动引用计数)也就是会自动释放不需要的内存
所有在Xcode4.2以上的版本使用MRC得在
=========>在文本框里的大上-fno-objc-arc
#import "ViewController.h"
#import "Student.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *array1 = [NSMutableArray array];
NSMutableArray *array2 = [NSMutableArray array];
[array1 addObject:array2];
[array2 addObject:array1];
//上面这个测试重复引用
//下面测试内存泄露
Student *student1=[[Student alloc]init];
NSLog(@"count1=%lu",(unsigned long)[student1 retainCount]);
Student *student2=[[Student alloc]init];
NSLog(@"count2=%lu",(unsigned long)[student2 retainCount]);
[student1 release];
NSLog(@"count1=%lu",(unsigned long)[student1 retainCount]);
Student *student3=[student2 copy];
NSLog(@"count3=%lu",(unsigned long)[student3 retainCount]);
if (student2 == student3)
{
NSLog(@"相同");
}
Student *student4=[student2 mutableCopy];
NSLog(@"count5=%lu",(unsigned long)[student4 retainCount]);//等于1
NSString *string=[NSString stringWithFormat:@"ppp"];
NSString *string1=[string copy];
if (string == string1)
{
NSLog(@"%lu,%lu",(unsigned long)[string retainCount],[string1 retainCount]);
}
NSMutableString *mstring=[NSMutableString stringWithFormat:@"ddd"];
NSMutableString *mstring1=[mstring copy];
NSLog(@"%lu,%lu",(unsigned long)[mstring retainCount],[mstring1 retainCount]);
//当我们将一个对象加入到集合(如数组,字典和set集合)中时,集合会拥有该对象的所有权,当集合移除该对象
//或者集合本身被清理是,集合会放弃对象的所有权
NSMutableArray *array=[NSMutableArray array];
for (int i=0; i<10; i++)
{
Student *stu=[Student new];
[array addObject:stu];
[stu release];
}
Student *st=[Student new];
[st release];
NSLog(@"%lu",(unsigned long)[st retainCount]);//本来预期输出是0为什么在这里会输出1呢
//已经释放的对象发送NSLog发送了一个消息
//在最后一次执行马上就要回收和就没有必有-1了不减1加快对象的释放
[student2 release];
[student3 release];
[student4 release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
在上面的代码通过Xcode->最上面的导航栏Product->Profile
会看的这张图片右边的图标
然后选择第二行第六个叫“Leaks”用来测试重复调用和内存泄露
- Objective-c语言_内存管理2(代码)
- objective-c语言_内存管理
- Objective-C_语言_内存管理2(代码)
- Objective-C语言:内存管理
- 黑马程序员—Objective-C基础_内存管理(2)
- Objective-C语言——内存管理2(代码篇)
- OBjective-c语言_文件管理
- objective-c 内存管理2
- Objective-C 内存管理2
- 黑马程序员—Objective-C基础_内存管理(1)
- iOS核心语言Objective C语言 —— 内存管理
- objective-c内存管理
- Objective-c 内存管理
- Objective-C内存管理~~
- Objective-C内存管理
- Objective-C 内存管理
- Objective-C 内存管理
- Objective-C 内存管理
- 自学PHP 笔记整理
- MySql中Lock命令和unlock命令
- UVA729 The Hamming Distance Problem
- WebUploader 手机端上传图片
- 传统bp算法类C/C++实现
- Objective-c语言_内存管理2(代码)
- MySQL 数据库字符集
- 结构型:设计模式之享元模式(八)
- IOS学习之IOS沙盒(sandbox)机制和文件操作
- WPF查找外部样式
- MySQL的lock tables和unlock tables的用法
- Segment 滑动
- JSONObject、JSONArray
- 音频混音算法的实现