OC17内存管理和自动引用计数

//
//  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
相关文章
相关标签/搜索