Does system release my objects when the application is terminated?

我的梦境 提交于 2019-12-24 10:15:01

问题


i always wonder sometimes that when i run my app an app pool is created and my app gets loaded inside the app pool and when i hit the home button my app is terminated i.e. the pool in which my app was residing will be terminated so in this case what will happen to those objects if i do not release such objects i think they will be terminated too but this methodology is not recommended by apple god knows for what reason. Can any gentleman provide me a solid answer for this question because it has really bugged me a lot these days..

Thanks & Regards


回答1:


Whenever your application is terminated the memory it had allocated is returned to the system. Autorelease pools don't play any role in this case, if that's what you mean by "application pool". The objects that are still in memory at that moment will not be deallocated (in the sense that system would call dealloc for you), the corresponding memory is simply marked as free.




回答2:


Each application has its own virtual memory address space in much the same way as you would expect for any "real" computer. When an app is terminated, its entire virtual memory disappears and the physical memory is reclaimed by the operating system.

So technically, when your app terminates there is no need to release any of the objects you currently have allocated but if you're managing your memory properly, you'll find it is actually hard to fail to release your objects.

AFAIK the term "application pool" has no meaning inside iOS.




回答3:


Autoreleasepools are created and destroyed many times during the runtime of your application ( at the beginnig and the end of a runloop in any case ).

When your application is terminated by the home button all the memory your application was using will be released and useable by any other app the same way you would expect that to work on your home computer.




回答4:


Apple docs clearly stated (the same in OSX) that on exiting o.s. simply reclaim ram. And in latest OSes also "applicationWillTerminate" behavior is affected similarly. The logic is that not useful to release part of a dying app.

(see also Are there drawbacks to Snow Leopard's new "sudden termination" mechanism? for OSX)

A different problem is if You need special behavior when you are quitting: - saving prefs - send a "goodbye" to a server..and so on.

Both cases are in my opinion wrong: - you must save prefs every time as apple suggests for user setting, - server should have a time out.

the same logic will cover the case app crashes.



来源:https://stackoverflow.com/questions/5885951/does-system-release-my-objects-when-the-application-is-terminated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!