`
shuai1234
  • 浏览: 972717 次
  • 性别: Icon_minigender_1
  • 来自: 山西
社区版块
存档分类
最新评论

iOS 5与iOS 6的 low-memory 处理

 
阅读更多

iOS 5与iOS 6的 low-memory 处理

分类: IOS 52人阅读 评论(0) 收藏 举报

from: http://www.cocoachina.com/newbie/basic/2013/0510/6171.html

 

移动设备终端的内存极为有限,应用程序必须做好low-memory处理工作,才能避免程序因内存使用过大而崩溃。

 

low-memory 处理思路
通常一个应用程序会包含多个view controllers,当从view跳转到另一个view时,之前的view只是不可见状态,并不会立即被清理掉,而是保存在内存中,以便下一次的快速显现。但是如果应用程序接收到系统发出的low-memory warning,我们就不得不把当前不可见状态下的views清理掉,腾出更多的可使用内存;当前可见的view controller也要合理释放掉一些缓存数据,图片资源和一些不是正在使用的资源,以避免应用程序崩溃。

 

思路是这样,具体的实施根据系统版本不同而略有差异,本文将详细说明一下iOS 5与iOS 6的low-memory处理。


iOS 5 的处理
在iOS 6 之前,如果应用程序接收到了low-memory警告,当前不可见的view controllers会接收到viewDidUnload消息(也可以理解为自动调用viewDidUnload方法),所以我们需要在 viewDidUnload 方法中释放掉所有 outlets ,以及可再次创建的资源。当前可见的view controller 通过didReceiveMemoryWarning 合理释放资源,具体见代码注释。


举一个简单的例子,有这样一个view controller:
@interface MyViewController : UIViewController {  
    NSArray *dataArray;  
}  
@property (nonatomic, strong) IBOutlet UITableView *tableView;  
@end 


对应的处理则为:
#pragma mark -
#pragma mark Memory management


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
    self.tableView = nil;
    dataArray = nil;
    
    [super viewDidUnload];
}


iOS 6 的处理
iOS 6 废弃了viewDidUnload方法,这就意味着一切需要我们自己在didReceiveMemoryWarning中操作。
具体应该怎么做呢?


1.将 outlets 置为 weak
当view dealloc时,没有人握着任何一个指向subviews的强引用,那么subviews实例变量将会自动置空。

@property (nonatomic, weak) IBOutlet UITableView *tableView;

 

2.在didReceiveMemoryWarning中将缓存数据置空
#pragma mark -   
#pragma mark Memory management   
  
  
- (void)didReceiveMemoryWarning  
{  
    [super didReceiveMemoryWarning];  
    // Dispose of any resources that can be recreated.   
    dataArray = nil;  

不要忘记一点,每当tableview reload 的时候,需要判断一下 dataArray ,若为空则重新创建。

 
兼容iOS 5 与 iOS 6
好吧,重点来了,倘若希望程序兼容iOS 5 与 iOS 6怎么办呢? 这里有一个小技巧,我们需要对didReceiveMemoryWarning 做一些手脚:

#pragma mark -
#pragma mark Memory management


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
    if ([self isViewLoaded] && self.view.window == nil) {
        self.view = nil;
    }
    
    dataArray = nil;
}


判断一下view是否是window的一部分,如果不是,那么可以放心的将self.view 置为空,以换取更多可用内存。


这样会是什么现象呢?假如,从view controller A 跳转到 view controller B ,然后模拟low-memory警告,此时,view controller A 将会执行self.view = nil ; 当我们从 B 退回 A 时, A 会重新调用一次 viewDidLoad ,此时数据全部重新创建,简单兼容无压力~~


Note:
如果你好奇Apple为什么废弃viewDidUnload,可以看看Apple 的解释:
Apple deprecated viewDidUnload for a good reason. The memory savings from setting a few outlets to nil just weren’t worth it and added a lot of complexity for little benefit. For iOS 6+ apps, you can simply forget about view unloading and only implement didReceiveMemoryWarning if the view controller can let go of cached data that you can recreate on demand later.

分享到:
评论

相关推荐

    HBuilderX uniapp打包内存溢出解决放案.zip

    uniapp项目体量过大时,打包H5会报错, - process out of memory 解决放案: 替换HBuildX内置\HBuilderX\plugins\node\node.exe版本 添加\HBuilderX\plugins\compile-node-sass\node_modules\node-sass-china\...

    High Performance iOS Apps: Optimize Your Code for Better Apps [2016]

    EPUB + PDF Ready to build mobile apps that out-perform the rest?... You’ll learn how to design and optimize iOS apps that deliver a smooth experience even when the network is poor and memory is low.

    iOS开发应用的开发和调试相关知识.rar

    3. Cocoa Touch:这是iOS应用开发的基础框架,提供了一系列UI组件和事件处理机制,如UIViewController、UILabel、UIButton等。 4. Model-View-Controller (MVC):MVC是一种设计模式,将应用分为模型、视图和控制器...

    iOS逆向之lldb常用操作指令.pdf

    iOS逆向工程是获取应用程序运行机制和探索其潜在漏洞的一种技术手段,而lldb(Little Low-Level Debugger)是在iOS逆向工程过程中用于调试和分析应用程序的重要工具。lldb提供了一系列强大的调试指令,使得开发者...

    NatCorder - Video Recording API 1.7.2.unitypackage

    NatCorder is heavily optimized for speed with a low memory footprint. Features include: - Blazing Fast. NatCorder is designed and heavily optimized for performance. - Record Anything! Record anything...

    Volumetric Light Beam v1.92

    - Super FAST w/ low memory footprint: doesn't require any post-process, command buffers, nor compute shaders: works great even on low-performance platforms such as Mobiles and WebGL. - Procedural ...

    Hello.Android.4th.Edition

    is optimized for low-power, low-memory devices in a fundamental way that no previous platform has attempted. • High-quality graphics and sound: Smooth, antialiased 2D and 3D-accelerated graphics have...

    ATMEL SAM9G45 原理图(ORCAD格式) PCB (ALLEGRO格式)及BOM清单 .rar

    SAM9G45: ARM926-based eMPU with DDR2, HS USB & SDIO and 3.3V IOs Atmel's SAM9G45 is a high-performance eMPU with the right peripheral mix and performance characteristics to make it the ideal ...

    NatCam - WebCam API 2.3.0.unitypackage

    NatCam - WebCam API 2.3.0 NatCam is a native camera API for iOS and Android that gives you full control over the cameras... NatCam is highly optimized for speed while maintaining a low memory footprint.

    Unity - NatCorder

    NatCorder is a lightweight, easy-to-use, full-feature video recording API for iOS and Android. With NatCorder, you can record the ... NatCorder is heavily optimized for speed with a low memory footprint

    IT新名词集锦 新名词 汇总

    与NOR Flash Memory相比,NAND Flash Memory具有更高的存储密度和更低的成本,但读取速度较慢。随着技术的进步,NAND Flash Memory已经成为便携式电子设备中最常用的存储解决方案之一。 ### 9. Low-K Materials **...

    CISCO 技术大集合

    同时由于路由器Router3除了与路由器Router2相连外,不再与其他路由器相连,所以也可以为它赋予一条默认路由以代替以上的二条静态路由, ip route 0.0.0.0 0.0.0.0 192.1.0.65 即只要没有在路由表里找到去特定目的...

    通过配置挂断音解决FXO口咬线

    5. 保存并应用配置:使用`write memory`或`copy running-config startup-config`命令保存配置更改,并确保设备重新加载配置。 6. 监控和测试:观察系统日志以确认挂断音是否被正确识别,同时进行实际通话测试,看...

    Android内核介绍

    这部分的实现位于`drivers/staging/android/lowmemorykiller.c`。 ##### 4. Ashmem (Anonymous Shared Memory) 为进程提供匿名共享内存的支持,可以提高进程间的通信效率。Ashmem的相关代码位于`mm/ashmem.c`。...

    Collections

    ### Collections in iOS Development #### About Collections In the realm of iOS development, collections play a crucial role in organizing and managing data. The Apple documentation on collections ...

    Advanced Apple Debugging & Reverse Engineering v0.9.5

    5. Expression Now that you’ve learned how to set breakpoints so the debugger will stop in your code, it’s time to get useful information out of whatever software you’re debugging. In this chapter ...

    ObjCRuntimeGuide

    Runtime functions allow developers to perform low-level operations, such as retrieving information about classes and methods, or even modifying them at runtime. These functions are part of the runtime...

    高性能地播放GIF功能

    源码UIImageView-PlayGIF,UIImageView-PlayGIF可以简单高性能地播放GIF,且很少占用内存。 测试环境:Xcode 5.0,iOS 5.0以上 UIImageView category/subclass ... Simple, high performance, low memory footprint.

    CCNA V26 题目

    在升级路由器的IOS版本之前,需要确保有足够的存储空间来存放新的软件,并且确认当前的硬件支持新版本。因此,正确的做法是: - **B. The amount of available flash and RAM memory**:检查可用的闪存和RAM空间是否...

Global site tag (gtag.js) - Google Analytics