解析iOS内存不足时的警告以及处理过程

2020-01-14 15:51:23王振洲
易采站长站为您分析iOS内存不足时的警告以及处理过程,包括View Controller和生命周期等相关方面的知识,需要的朋友可以参考下  

内存警告
ios下每个app可用的内存是被限制的,如果一个app使用的内存超过了这个阀值,则系统会向该app发送Memory Warning消息。收到消息后,app必须尽可能多的释放一些不必要的内存,否则OS会关闭app。
几种内存警告级别(便于理解内存警告之后的行为)
 Memory warning level:

 

复制代码
typedef enum {
                     OSMemoryNotificationLevelAny      = -1,
                     OSMemoryNotificationLevelNormal   =  0,
                     OSMemoryNotificationLevelWarning  =  1,
                     OSMemoryNotificationLevelUrgent   =  2,
                     OSMemoryNotificationLevelCritical =  3
                     }OSMemoryNotificationLevel;(5.0以后废弃了)

 

            1、Warning (not-normal) — 退出或者关闭一些不必要的后台程序 e.g. Mail
            2、Urgent — 退出所有的后台程序 e.g. Safari and iPod.
            3、Critical and beyond — 重启

响应内存警告:
在应用程序委托中实现applicationDidReceiveMemoryWarning:方法:
            应用程序委托对象中接收内存警告消息
在您的UIViewController子类中实现didReceiveMemoryWarning方法:
            视图控制器中接收内存警告消息
注册UIApplicationDidReceiveMemoryWarningNotification通知:
            其它类中使用通知接收内存警告消息(例如:清理缓存数据)