14-ARC自动引用内存使用及管理机制
来源:互联网 发布:惊天动地影评知乎 编辑:程序博客网 时间:2024/06/06 01:32
// main.m
// ARC的基本使用
//
// Created by amesty on 15-1-8.
// Copyright (c) 2015年 itcast. All rights reserved.
//
/* ARC的基本原理
(一)ARC的判断准则:
只要没有强指针指向对象,对象就会被释放。
(二)指针分类:
(1)强指针:默认的情况下,所有的指针都是强指针,关键字_strong(如果没有强指针指向对象,对象就会自动被清空)
(2)弱指针:__weak关键字修饰的指针(只要弱指针指向的对象不在了,就直接把弱指针做清空操作)
声明一个弱指针如下:
_weak Person *p;
ARC中,只要弱指针指向的对象不在了,就直接把弱指针做清空操作。
_weak Person *p=[[Person alloc] init];//不合理,对象一创建出来就被释放掉,对象释放掉后,ARC把指针自动清零。
ARC中在property处不再使用retain,而是使用strong,在dealloc中不需要再[super dealloc]。
@property(nonatomic,strong)Dog *dog;// 意味着生成的成员变量_dog是一个强指针,相当于以前的retain。
如果换成是弱指针,则换成weak,不需要加_ _。
(三)ARC的特点总结:以前的retain改成strong
(1)不允许调用release,retain,retainCount
(2)允许重写dealloc,但是不允许调用[super dealloc]
(3)@property的参数:
Strong:相当于原来的retain(适用于OC对象类型),成员变量是强指针
Weak:相当于原来的assign,(适用于oc对象类型),成员变量是弱指针
Assign:适用于非OC对象类型(基础类型)
*/
#import <Foundation/Foundation.h>
#import"Person.h"
#import "Dog.h"
int main(int argc, const char * argv[])
{
Dog *d=[[Dog alloc]init];
Person *p=[[Person alloc]init];
p.dog=d;
d=nil;
NSLog(@"%@",p.dog);
return 0;
}
void weekpointtest()
{
//不合理,对象一创建出来就被释放掉,对象释放掉后,ARC把指针自动清零。
__weak Person *p=[[Person alloc] init];
NSLog(@"-------");
NSLog(@"%@",p);
}
=================
// Person.h
// ARC的基本使用
//
// Created by amesty on 15-1-8.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@class Dog;
@interface Person : NSObject
@property (nonatomic,strong)Dog * dog;
@property(nonatomic,strong)NSString * name;
+(void)dealloc;
@end
====================
// Person.m
// ARC的基本使用
//
// Created by amesty on 15-1-8.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import "Person.h"
@implementation Person
+(void)dealloc
{
NSLog(@"Person对象被释放");
[super dealloc];
}
@end
// Dog.h
// ARC的基本使用
//
// Created by amesty on 15-1-8.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import "Person.h"
@interface Dog : NSObject
@end
// Dog.m
// ARC的基本使用
//
// Created by amesty on 15-1-8.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import "Dog.h"
@implementation Dog
@end
// main.m
// ARC内存管理机制
//
// Created by amesty on 15-1-8.
// Copyright (c) 2015年 itcast. All rights reserved.
//
/*
1.autorelease的基本用法
1> 会将对象放到一个自动释放池中
2> 当自动释放池被销毁时,会对池子里面的所有对象做一次release操作
3> 会返回对象本身
4> 调用完autorelease方法后,对象的计数器不变
2.autorelease的好处
1> 不用再关心对象释放的时间
2> 不用再关心什么时候调用release
3.autorelease使用注意
1> 占用内存较大的对象不要随便使用autorelease
2> 占用内存较小的对象使用autorelease,没有太大影响
4.错误写法
1> alloc之后调用了autorelease,又调用release
@autoreleasepool {
Person *p = [[[Person alloc] init] autorelease];
[p release];
}
2> 连续调用多次autorelease
@autoreleasepool {
Person *p = [[[[Person alloc] init] autorelease] autorelease];
}
5.自动释放池
1> 在iOS程序运行过程中,会创建无数个池子。这些池子都是以栈结构存在(先进后出)
2> 当一个对象调用autorelease方法时,会将这个对象放到栈顶的释放池
6.自动释放池的创建方式
1> iOS 5.0前(NSAutoreleasePool)
2> iOS 5.0后(@autorelease)
1.系统自带的方法里面没有包含alloc、new、copy,说明返回的对象都是autorelease的
2.开发过程中经常会提供一些类方法,快速创建一个已经autorelease过的对象
1> 创建对象时不要直接用类名,一般用self
+ (id)person
{
return [[[self alloc] init] autorelease];
}
*/
#import <Foundation/Foundation.h>
#import"Person.h"
#import "RichPerson.h"
int main(int argc, const char * argv[])
{
@autoreleasepool{
RichPerson *p=[RichPerson personWithAge:10];
p.money=100;
}
return 0;
}
void test()
{
@autoreleasepool { //开始代表创建了释放池
//autorelease方法会返回对象本身 池子是用栈这种数据结构存放数据内容的(先进后出)
//autorelease会将对象放在一个自动释放池中
//当自动释放池被销毁时,会对池子里面的所有对象做一次release操作
Person *p=[[[Person alloc]init] autorelease];
p.age=10;
// insert code here...
NSLog(@"Hello, World!");
@autoreleasepool
{
Person *p2=[[[Person alloc]init] autorelease];
p2.age=20;
}
} //结束代表销毁释放池
}
void test2()
{
@autoreleasepool
{
Person *p =[[[Person alloc] init] autorelease];
}
NSLog(@"test");
}
void test3()
{
@autoreleasepool {
Person *p=[Person Person];
p.age=100;
Person *p1=[Person personWithAge:200];
NSLog(@"Hello, World!");
}
}
==========
//
// Person.h
// ARC内存管理机制
//
// Created by amesty on 15-1-8.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property(nonatomic,assign)int age;
+(id)Person;
+(id)personWithAge:(int)age;
@end
//
// Person.m
// ARC内存管理机制
//
// Created by amesty on 15-1-8.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import "Person.h"
/*
1.系统自带的方法里面没有包含alloc、new、copy,说明返回的对象都是autorelease的
2.开发过程中经常会提供一些类方法,快速创建一个已经autorelease过的对象
1> 创建对象时不要直接用类名,一般用self
+ (id)person
{
return [[[self alloc] init] autorelease];
}
*/
@implementation Person
-(void)dealloc
{
NSLog(@"%d对象销毁",_age);
[super dealloc];
}
+(id)Person;
{ //这样改写之后谁调用就创建什么样的对象,而不是限定于给类本身,任何子类可以据此创建子类对象呢。
//return [[[Person alloc]init]autorelease];
return [[[self alloc]init]autorelease];
}
+(id)personWithAge:(int)age
{
// Person * p1=[[[Person alloc]init]autorelease];
Person * p1=[self Person];
p1.age=age;
return p1;
}
@end
=======================
// RichPerson.h
// ARC内存管理机制
//
// Created by amesty on 15-1-8.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import "Person.h"
@interface RichPerson : Person
@property (nonatomic,assign) int money;
@end
// RichPerson.m
// ARC内存管理机制
//
// Created by amesty on 15-1-8.
// Copyright (c) 2015年 itcast. All rights reserved.
//
#import "RichPerson.h"
@implementation RichPerson
@end
- 14-ARC自动引用内存使用及管理机制
- 大多数情况下使用ARC内存自动管理机制,特殊情况采用MRC手动管理
- ARC内存管理机制
- Swift ARC-自动引用计数、内存管理
- Java自动内存管理机制(三) 对象、引用
- iOS内存管理机制及属性的使用
- 自动内存管理机制
- Java自动内存管理机制
- 自动内存管理机制
- JVM_自动内存管理机制
- JVM 自动内存管理机制
- 自动内存管理机制
- java自动内存管理机制
- 自动内存管理机制
- 自动内存管理机制
- Java 自动内存管理机制
- 自动内存管理机制_HotSpot
- JVM自动内存管理机制
- Java数组声明、创建、初始化
- cocos2dx--两个场景切换各函数调用顺序
- @class
- 报错:loaded the "" nib but didn't get a UITableView
- Server.Transfer和AjaxPro不兼容问题
- 14-ARC自动引用内存使用及管理机制
- android中jsoup解析html的几个例子
- jqGrid学习
- wordpress ueditor编辑器的使用
- maven资料
- hdu---1722 Cake
- MySQL几点重要的性能指标计算和优化
- 【微软100题】002设计包含 min 函数的栈
- 文件was built