Objective-c中提供了两种内存管理机制MRC(MannulReference Counting)和ARC(Automatic Reference Counting),分别提供对内存的手动和自动管理,来满足不同的需求。注意的是Xcode 4.1及其以前版本没有ARC,需要理解MRC,但实际使用时强推ARC。
ARC转换回MRC 方法
Student.h
#import <Foundation/Foundation.h>
@interface Student : NSObject
@end
Student.m
#import "Student.h"
@implementation Student
//copyWithZone要遵守<NSCopying>协议
-(instancetype)copyWithZone:(NSZone *)zone
{
Student *student = [Student new];
return student;
}
-(id)mutableCopy
{
Student *student = [Student new];
return student;
}
@end
ViewController.m
#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];
// NSLog(@"%@",array2);
NSObject *object = [[NSObject alloc]init];
NSLog(@"objectCount = %zd",[object retainCount]);
// [object release];
NSObject *anotherObject = [object retain]; //Retain是创建一个指针,引用对象计数加1。
NSLog(@"anotherObjectCount = %zd",[object retainCount]);
[anotherObject release];
NSLog(@"anotherObjectCount = %zd",[object retainCount]);
[object release]; //计算对象的引用使用后释放
Student *student1 = [[Student alloc] init];
NSLog(@"student1Count = %zd",[student1 retainCount]);
Student *student2 = [student1 retain];
NSLog(@"student1Count =%zd",student2.retainCount);
// [student2 release];
NSLog(@"student2Count = %zd",student2.retainCount);
Student *student3 = [student2 copy]; //浅拷贝
NSLog(@"student3Count = %zd",student3.retainCount);
if (student2 ==student3) {
NSLog(@"相同");
}
[student1 release];
// [student2 release];//计算对象的引用使用后释放
[student3 release];
Student *student4 = [student2 mutableCopy]; //深拷贝
NSLog(@"student4count = %zd ",student4.retainCount);
[student2 release];//计算对象的引用使用后释放
[student4 release];
NSString *string = [NSString stringWithFormat:@"123"];
NSString *string1 = [string copy];
if (string == string1) {
NSLog(@"string = %lu string1 = %lu",[string retainCount],[string1 retainCount]);
}
//当我们将一个对象加入集合(如数组、字典和 set 集合)中时,集合会拥有该对象的所有权;当结合移除该对象,或者集合本身被清理时,集合会放弃对象的所有权.
NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < 10; i++) {
Student *stu = [Student new];
[array addObject:stu];
[stu release];
NSLog(@"stuCount[%d] = %zd",i,[stu retainCount]);
}
Student *st = [Student new];
[st release];
NSLog(@"stCount = %zd",[st retainCount]);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
运行结果
2016-01-03 15:56:12.284 OC_LX_11[7453:205581] objectCount = 1
2016-01-03 15:56:12.285 OC_LX_11[7453:205581] anotherObjectCount = 2
2016-01-03 15:56:12.286 OC_LX_11[7453:205581] anotherObjectCount = 1
2016-01-03 15:56:12.286 OC_LX_11[7453:205581] student1Count = 1
2016-01-03 15:56:12.286 OC_LX_11[7453:205581] student1Count =2
2016-01-03 15:56:12.287 OC_LX_11[7453:205581] student2Count = 2
2016-01-03 15:56:12.287 OC_LX_11[7453:205581] student3Count = 1
2016-01-03 15:56:12.287 OC_LX_11[7453:205581] student4count = 1
2016-01-03 15:56:12.287 OC_LX_11[7453:205581] string = 18446744073709551615 string1 = 18446744073709551615
2016-01-03 15:56:12.288 OC_LX_11[7453:205581] stuCount[0] = 1
2016-01-03 15:56:12.288 OC_LX_11[7453:205581] stuCount[1] = 1
2016-01-03 15:56:12.288 OC_LX_11[7453:205581] stuCount[2] = 1
2016-01-03 15:56:12.289 OC_LX_11[7453:205581] stuCount[3] = 1
2016-01-03 15:56:12.289 OC_LX_11[7453:205581] stuCount[4] = 1
2016-01-03 15:56:12.289 OC_LX_11[7453:205581] stuCount[5] = 1
2016-01-03 15:56:12.290 OC_LX_11[7453:205581] stuCount[6] = 1
2016-01-03 15:56:12.290 OC_LX_11[7453:205581] stuCount[7] = 1
2016-01-03 15:56:12.388 OC_LX_11[7453:205581] stuCount[8] = 1
2016-01-03 15:56:12.388 OC_LX_11[7453:205581] stuCount[9] = 1
2016-01-03 15:56:12.388 OC_LX_11[7453:205581] stCount = 1