什么样的“僵尸”?应用程序可能会从后台返回?
目前,我正在为应用程序商店构建我的第一个应用程序,因此我已阅读此处,应用程序应使用少于 30 MB 的 RAM(iPhone 3G、iPhone 4)。我读到,在某个时间点,iOS 将开始通过随机杀死和释放后台应用程序中的声音、图像和其他资源来寻找资源。目前,我的应用程序使用仪器中显示的 ~5MB 实时字节,但我对内存感到有些偏执:) 当应用程序针对 iPhone3G 和 iPhone4 使用少于 10MB 时,我安全吗?
问题是,如果一个应用程序进入后台,那么 iOS 可以释放它多少资源,直到最终杀死它?如果我的应用程序进入后台,然后将启动 100 个或更多其他应用程序,那么我的应用程序会发生什么情况?我不相信 RAM 内存对于我的应用程序来说是静态的,因为每个设备中的内存都是有限的。恕我直言,如果你开始循环打开一个新应用程序,将其发送到后台,打开另一个应用程序 - 那么设备 RAM 将在某个时间点被完全使用。然后,理论上,如果你尝试打开一个新应用程序,那么一些最先打开的应用程序应该被 iOS 杀死...
目前,我正在使用纯 UIKit 构建小游戏,因此我使用了大量 UIView 和 UIViewImage 对象,并且我不知道如何处理这种理论上的情况。在进入后台期间,我的应用程序可能会加载很多游戏中的 UIViewImages、指向菜单 MVC 的指针等。我是否必须编写一些 reloadALL 方法来重新加载游戏的每个和平?如果设备内存被完全使用,iOS 会杀死我的整个应用程序,那么一切都会好起来的。但如果iOS会在游戏或菜单对象中释放我的一些UIViewImage,这是不可接受的。在这种情况下,我不知道我的应用程序可能是哪种“僵尸”(我的应用程序在“复活”后可能有多少条腿、手臂等)。请分享您的经验和想法:)
Currently, I'm building my first app for the app store therefore I have read here, that an app should use less than 30 MB of RAM (iPhone 3G, iPhone 4). I have read that, at some point in time, iOS will start hunting for the resources by randomly killing and freeing sounds, images and other resources from the background apps. Currently, my app uses ~5MB of live bytes that are showed in instruments but I feel some paranoia about memory :) Am I safe when app uses less that 10MB when targeting iPhone3G and iPhone4?
The question is if an app goes to background then how much of resources iOS can free of it until finally killing it? If my app goes to background and then 100 or more others apps will be launched then what happens to my app? I do not believe that RAM memory piece is static for my app because the memory in each device is limited. IMHO, if you start looping with opening a new app, sending it to the background, opening another one - then device RAM will be totally used at some point in time. Then, theoretically, if you try to open a new app then some very first opened apps should be killed by iOS...
Currently, I'm building small game with pure UIKit, therefore I use a lot of UIView and UIViewImage objects and I'm not sure how to deal with this theoretical situation. During entering the background, my app might gave loaded a lot of in game UIViewImages, pointer to menu MVC and etc. Do I have to code some reloadALL method for reloading every peace of the game? Everything would be OK if iOS would kill my whole app if device memory is totally used. But it would be unacceptable if iOS would release some of my UIViewImage in game or menu objects. In that case, I don't know what kind of "zombie" (how many legs, arms and etc my app could have after a "resurrection") my app could be. Please share your experience and thoughts :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一旦应用程序进入后台并且系统需要更多内存,它只会杀死后台应用程序,并且您的应用程序在发生这种情况之前不会收到任何消息。所以要么全有要么全无。
尽管如此,iOS 的内存管理支持视图对象的释放和延迟重新分配,以防出现内存警告(当您的应用程序仍处于活动状态时,请检查 UIViewController docs
didRecieveMemoryWarning
系统本身如何处理内存警告)现在,当您的应用程序进入后台时,您可以尝试实现类似的行为 - 也释放一些内存(例如,您自己的缓存或元素可以是延迟重新创建),因此应用程序的内存占用量会减少。这样做可能有助于让您的应用在被杀死之前在后台运行一段时间。
当 iOS 开始杀死应用程序时,它可能会使用自己的算法,这不一定会杀死最旧的应用程序 - 它也可能会选择内存方面最大的应用程序有较大影响...
Once an app has entered background and the system needs more memory, it just kills the backgrounded app and your app will not get any message before this happens. So its all or nothing.
Nevertheless the memory management of iOS supports e.g. deallocation of view objects and lazy recreation in case of memory warnings (while your app is still active, check UIViewController docs
didRecieveMemoryWarning
how the system itself copes with memory warnings)Now when your app enters background you could try to implement similar behavior - also free some memory (e.g. your own caches or elements which can be lazily recreated) so the memory footprint of your app gets reduced. Doing so might help to have your app live a litte longer in the background before it gets killed.
When iOS starts to kill apps, it might use its own algorithm, which is not neccessarily killing the oldest app - it might also go for the largest app in terms of memory to have a larger effect...
iOS 将在后台必要时剔除 UIKit 数据(例如图像视图)和任何内存中的 NSCache。不过,您不必担心恢复这一点。
但是,您仍然应该管理自己的对象和东西;当您收到适当的事件(例如内存警告)时,处理剔除和恢复数据。
iOS will cull UIKit data (such as your image views) and any in-memory NSCaches when necessary when backgrounded. You do not need to worry about restoring this though.
However, you should still manage your own objects and stuff; dealing with culling and restoring data when you receive appropriate events such as memory warnings.
我们不应该真正关心应用程序中使用的总图像。内存仅用于活动屏幕中的那些图像。
We should not be really concerned about the total images used in the app. The memory is used for only those images in active screen.