标签:
//
// ViewController.m
// OC17内存管理和自动引用计数
//
// Created by Zoujie on 15/10/25.
// Copyright ? 2015年 Zoujie. All rights reserved.
//
#import "ViewController.h"
#import "Fraction.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
#pragma mark 内存管理的基本知识
//应用没有足够的内存去坐更多地事情,就有崩溃的可能。内存管理关心的是清理(回收)不用的内存,以便内存能够再次利用。
// 1.释放不在使用对象的内存
// 2.确定对象不在需要使用
// 3.引用计数(手动,用于支持遗留的非ARC工程)
[self managerMemoryByMyself];
// 4.Xcode4.2以后 ARC 自动引用计数
[self autoARC];
}
-(void)managerMemoryByMyself
{
// 当创建对象时,初始的引用计数为1
// 每当创建引用到对象需要为引用对象+1 ,发送retain消息
id obj,obj1;
// [obj retain];
// 当不需要对象时,通过给对象发送release消息 ,为引用计数减1
// [obj release];
// 当引用计数为0的时候,系统就知道这个对象不在需要使用了
// add方法 会增加引用计数
[obj addObject:obj1];
[obj addSubview:obj1];
// remove会减少对象引用计数
[obj removeObjectAtIndex:0];
[obj removeFromSuperview];
// NSAutoreleasePool 自动释放池可以帮助追踪需要延迟一些时间释放的对象,通过给自动释放池发送drain消息,自动释放池会被清理,对象会被释放
// [obj1 autorelease];
Fraction *frac1 = [[Fraction alloc]init];
Fraction *frac2 = [[Fraction alloc]init];
// [frac1 release];
// [frac2 release];
}
-(void)autoARC
{
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
标签:
原文地址:http://my.oschina.net/u/2319073/blog/521924