iOS extracts: Low Memory Warnings


extracts from 《Beginning iPhone Games Development》

One thing to keep in mind when developing for iPhone is that your game’s available RAMis limited. Typically, iPhone OS uses about 40MB of RAM. This leaves you with less than24MB of RAM available when running your game on an iPhone 2G or 3G. The situationwith iPod touch models is a bit better. Because the iPod touch doesn’t need to receivephone calls or text messages, the operating system uses less RAM than on an iPhone.You have about 32MB of RAM available when running your game on an iPod touch.

The important thing to remember is that your game will be forcibly shut down by theoperating system if it uses too much of the available RAM. If the operating system killsyour app, it's too late to start worrying about why it happened. It is likely you have amemory leak somewhere, and you will need to spend some time debugging the problem. 

Cocoa Touch does try to help, though. There is a message received by the app delegatethat lets you try to free up memory before it's too late. The message calls a methodnamed applicationDidReceiveMemoryWarning:. You should use theapplicationDidReceiveMemoryWarning: method to release any cached data yourapplication may hold in the app delegate.

For example, if your game uses data from a web service, you can cache that data thefirst time you retrieve it from the web service. Subsequent uses of the web service datacan access the cache, rather than hitting the web service every time. The app delegateis an ideal place to hold this data, and the applicationDidReceiveMemoryWarning:method should release this cache when memory is low. If your app needs the webservice data again, it can reload it from the web service and cache it until memory is lowagain.

View controllers also receive a similar message, with call to the method namedviewDidUnload. Over the course of a game, you are likely to load several views, one foreach screen in your game. As you navigate from one screen to the other, it is likely theother screens don’t need to take up RAM while they are not visible. Cocoa Touch callsthe viewDidUnload method after a view is removed from the screen. If your viewallocates any objects in memory, it should release them in the viewDidUnload method. 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值