Chinaunix首页 | 论坛 | 博客
  • 博客访问: 397318
  • 博文数量: 466
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-16 13:59
文章分类

全部博文(466)

文章存档

2015年(466)

我的朋友

分类:

2015-03-16 14:31:42

Observing Low-Memory Warnings

When the system dispatches a low-memory warning to your application, heed it. iOS notifies the frontmost application whenever the amount of free memory dips below a safe threshold. If your application receives this warning, it must free up as much memory as it can by releasing objects it does not need or clearing out memory caches that it can easily recreate later.

UIKit provides several ways to receive low-memory warnings, including the following:

Upon receiving any of these warnings, your handler method should respond by immediately freeing up any unneeded memory. For example, the UIViewController class responds by automatically purging its view if that view is not currently visible; subclasses can supplement the default behavior by purging additional data structures. An application that maintains a cache of images might respond by releasing any images that are not currently onscreen.

If your custom objects have known purgeable resources, you can have those objects register for theUIApplicationDidReceiveMemoryWarningNotification notification and release their purgeable resources directly. Have these objects register if you have a few objects that manage most of your purgeable resources and if it is appropriate to purge all of those resources. But if you have many purgeable objects or want to coordinate the release of only a subset of those objects, you might want to use your application delegate to release the desired objects.

Important: Like the system applications, your applications should always handle low-memory warnings. System applications consume small amounts of memory while processing requests. When a low-memory condition is detected, the system delivers low-memory warnings to all running programs (including your application) and may terminate some background applications (if necessary) to ease memory pressure. If not enough memory is released—perhaps because your application is leaking or still consuming too much memory—the system may still terminate your application.

You can test your application’s behavior under low-memory conditions using the Simulate Memory Warning command in the simulator.


阅读(260) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~