关于ios不同版本间的内存管理差异和iso6与以前版本内存管理的兼容
来源:互联网 发布:通达信软件怎么样 编辑:程序博客网 时间:2024/04/29 10:32
官方文档讲解的是比较详细的:
https://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.html
模拟内存警告:
有三种方法可以实现内存警告。
1.模拟器菜单:Hardware-》Simulate Memory Warning
2.用程序的方法实现,只需要一句代码:
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), (CFStringRef)@"UISimulatedMemoryWarningNotification", NULL, NULL, true);
3.这是私有api方法:
SEL memoryWarningSel = @selector(_performMemoryWarning);
if ([[UIApplication sharedApplication] respondsToSelector:memoryWarningSel]) {
[[UIApplication sharedApplication] performSelector:memoryWarningSel];
}else {
NSLog(@"%@",@"Whoops UIApplication no loger responds to -_performMemoryWarning");
}
内存警告的处理:
自从iPhone4 支持多任务后,我们需要更加仔细处理内存不足的情形。如果用户运行我们程序的时候,后台还跑着N个软件,那前台运行的iphone 程序就很容易收到内存不足的警告。
通常情况下,iOS在内存不足时会给用户一次处理内存资源的机会。当我们的程序在第一次收到内存不足警告时,应该释放一些不用的资源,以节省部分内存。否则,当内存不足情形依然存在,iOS再次向我们程序发出内存不足的警告时,我们的程序将会被iOS kill掉。
iOS的UIViewController 类给我们提供了处理内存不足的接口。在iOS 3.0 之前,当系统的内存不足时,UIViewController的didReceiveMemoryWarining 方法会被调用,我们可以在didReceiveMemoryWarining 方法里释放掉部分暂时不用的资源。
从iOS3.0 开始,UIViewController增加了viewDidUnload方法。该方法和viewDIdLoad相配对。当系统内存不足时,首先UIViewController的didReceiveMemoryWarining 方法会被调用,而didReceiveMemoryWarining 会判断当前ViewController的view是否显示在window上,如果没有显示在window上,则didReceiveMemoryWarining 会自动将viewcontroller 的view以及其所有子view全部销毁,然后调用viewcontroller的viewdidunload方法。如果当前UIViewController的view显示在window上,则不销毁该viewcontroller的view,当然,viewDidunload也不会被调用了。
但是到了ios6.0之后,这里又有所变化,ios6.0内存警告的viewDidUnload 被屏蔽,即又回到了ios3.0的时期的内存管理方式。
iOS3-iOS6.0以前版本收到内存警告:
调用didReceiveMemoryWarning内调用super的didReceiveMemoryWarning会将controller的view进行释放。所以我们不能将controller的view再次释放。
处理方法:
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];//如没有显示在window上,会自动将self.view释放。
// ios6.0以前,不用在此做处理,self.view释放之后,会调用下面的viewDidUnload函数,在viewDidUnload函数中做处理就可以了。
}
-(void)viewDidUnload
{
// Release any retained subviews of the main view.不包含self.view
[super viewDidUnload];
//处理一些内存和资源问题。
}
iOS6.0及以上版本的内存警告:
调用didReceiveMemoryWarning内调用super的didReceiveMemoryWarning调只是释放controller的resouse,不会释放view
处理方法:
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];//即使没有显示在window上,也不会自动的将self.view释放。
// Add code to clean up any of your own resources that are no longer necessary.
// 此处做兼容处理需要加上ios6.0的宏开关,保证是在6.0下使用的,6.0以前屏蔽以下代码,否则会在下面使用self.view时自动加载viewDidLoad
if ([self.view window] == nil)// 是否是正在使用的视图
{
// Add code to preserve data stored in the views that might be
// needed later.
// Add code to clean up other strong references to the view in
// the view hierarchy.
self.view = nil;// 目的是再次进入时能够重新加载调用viewDidLoad函数。
}
}
https://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.html
模拟内存警告:
有三种方法可以实现内存警告。
1.模拟器菜单:Hardware-》Simulate Memory Warning
2.用程序的方法实现,只需要一句代码:
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), (CFStringRef)@"UISimulatedMemoryWarningNotification", NULL, NULL, true);
3.这是私有api方法:
SEL memoryWarningSel = @selector(_performMemoryWarning);
if ([[UIApplication sharedApplication] respondsToSelector:memoryWarningSel]) {
[[UIApplication sharedApplication] performSelector:memoryWarningSel];
}else {
NSLog(@"%@",@"Whoops UIApplication no loger responds to -_performMemoryWarning");
}
内存警告的处理:
自从iPhone4 支持多任务后,我们需要更加仔细处理内存不足的情形。如果用户运行我们程序的时候,后台还跑着N个软件,那前台运行的iphone 程序就很容易收到内存不足的警告。
通常情况下,iOS在内存不足时会给用户一次处理内存资源的机会。当我们的程序在第一次收到内存不足警告时,应该释放一些不用的资源,以节省部分内存。否则,当内存不足情形依然存在,iOS再次向我们程序发出内存不足的警告时,我们的程序将会被iOS kill掉。
iOS的UIViewController 类给我们提供了处理内存不足的接口。在iOS 3.0 之前,当系统的内存不足时,UIViewController的didReceiveMemoryWarining 方法会被调用,我们可以在didReceiveMemoryWarining 方法里释放掉部分暂时不用的资源。
从iOS3.0 开始,UIViewController增加了viewDidUnload方法。该方法和viewDIdLoad相配对。当系统内存不足时,首先UIViewController的didReceiveMemoryWarining 方法会被调用,而didReceiveMemoryWarining 会判断当前ViewController的view是否显示在window上,如果没有显示在window上,则didReceiveMemoryWarining 会自动将viewcontroller 的view以及其所有子view全部销毁,然后调用viewcontroller的viewdidunload方法。如果当前UIViewController的view显示在window上,则不销毁该viewcontroller的view,当然,viewDidunload也不会被调用了。
但是到了ios6.0之后,这里又有所变化,ios6.0内存警告的viewDidUnload 被屏蔽,即又回到了ios3.0的时期的内存管理方式。
iOS3-iOS6.0以前版本收到内存警告:
调用didReceiveMemoryWarning内调用super的didReceiveMemoryWarning会将controller的view进行释放。所以我们不能将controller的view再次释放。
处理方法:
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];//如没有显示在window上,会自动将self.view释放。
// ios6.0以前,不用在此做处理,self.view释放之后,会调用下面的viewDidUnload函数,在viewDidUnload函数中做处理就可以了。
}
-(void)viewDidUnload
{
// Release any retained subviews of the main view.不包含self.view
[super viewDidUnload];
//处理一些内存和资源问题。
}
iOS6.0及以上版本的内存警告:
调用didReceiveMemoryWarning内调用super的didReceiveMemoryWarning调只是释放controller的resouse,不会释放view
处理方法:
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];//即使没有显示在window上,也不会自动的将self.view释放。
// Add code to clean up any of your own resources that are no longer necessary.
// 此处做兼容处理需要加上ios6.0的宏开关,保证是在6.0下使用的,6.0以前屏蔽以下代码,否则会在下面使用self.view时自动加载viewDidLoad
if ([self.view window] == nil)// 是否是正在使用的视图
{
// Add code to preserve data stored in the views that might be
// needed later.
// Add code to clean up other strong references to the view in
// the view hierarchy.
self.view = nil;// 目的是再次进入时能够重新加载调用viewDidLoad函数。
}
}
0 0
- 关于ios不同版本间的内存管理差异和iso6与以前版本内存管理的兼容
- 关于ios不同版本间的内存管理差异和iso6与以前版本内存管理的兼容
- 关于ios不同版本间的内存管理差异和iso6与以前版本内存管理的兼容
- 关于ios不同版本间的内存管理差异和iso6与以前版本内存管理的兼容
- 不同版本iOS的特性和差异
- Python3.x和以前版本的差异
- 关于Spark 1.5 版本中Spark自己管理内存而不是由Java管理内存的解释
- cococs2d-x与ios关于图片内存的管理
- iOS的内存管理
- IOS的内存管理
- iOS的内存管理
- iOS的内存管理
- iOS的内存管理
- iOS的内存管理
- iOS的内存管理
- iOS的内存管理
- iOS关于block的内存管理
- 关于iOS内存管理的规则思考
- WebService学习之旅(七)Axis2发布WebService的几种方式
- jar5
- iOS开发中@selector的理解与应用
- mybatis 错误
- Jumb
- 关于ios不同版本间的内存管理差异和iso6与以前版本内存管理的兼容
- Android 成长之路(1)——创建第一个App
- Material Design实战
- android spanner下拉列表内容居中
- 关于ARC需要了解的一些基本知识点
- Windows环境下QWT安装及配置
- 数据库范式
- MAC 乱码设置
- java.text.MessageFormat类的学习