Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4914089
  • 博文数量: 763
  • 博客积分: 12108
  • 博客等级: 上将
  • 技术积分: 15717
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-28 21:21
个人简介

业精于勤,荒于嬉

文章分类

全部博文(763)

文章存档

2018年(6)

2017年(15)

2016年(2)

2015年(31)

2014年(14)

2013年(87)

2012年(75)

2011年(94)

2010年(190)

2009年(38)

2008年(183)

2007年(28)

分类:

2010-09-24 17:21:32

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.


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