1

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

zoul
  • 102,279
  • 44
  • 260
  • 354
Radix
  • 3,639
  • 3
  • 31
  • 47

4 Answers4

7

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.

zoul
  • 102,279
  • 44
  • 260
  • 354
  • 1
    ya so in this case if i dont release the object that i owned that memory will be returned to the system when i terminate my app am i correct on this – Radix May 04 '11 at 15:40
  • Then why does apple recommends that we should release the objects that we owned because anyways the memory allocated will be returned to the system on app termination – Radix May 04 '11 at 15:42
  • 2
    Usually the application allocates more and more objects while it's running, and as the amount of available memory is pretty small, you would eventually run out of memory if you did not release the objects as soon as possible. – zoul May 04 '11 at 15:47
  • Hmm this makes sense great zoul a thumbs up for your answer thanks for helping me out as this thing was going crazy inside my head – Radix May 04 '11 at 15:49
1

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.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • ya like i too felt the same Jeremy but when i saw zouls answer that really made sense to me, like it might happen that you are running your app in a finite memory zone and suddenly poof your app crashes because you forgot to manage memory for your app... – Radix May 04 '11 at 15:54
  • @Radix: your question was about what happens when your app is terminated. I was telling you what happens and why when your app gets terminated. – JeremyP May 04 '11 at 15:58
  • ok buddy thanks its because of developers like you due to which i have learned a lot about iOS else i was not getting any help. thanks for your support pal – Radix May 04 '11 at 16:01
0

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.

Antwan van Houdt
  • 6,989
  • 1
  • 29
  • 52
  • Hmmm that part is understood sir but if this is the case then why to release the objects that we alloc and init, i may be sounding like a noob here but please if you could put some light on this topic it would be very helpful to me sir – Radix May 04 '11 at 15:44
  • 1
    You need to release those objects because the amount of memory you have is finite. This means that when you use a certain amount of memory that your iphone application will quit, because there is no memory left for your application. – Antwan van Houdt May 04 '11 at 15:46
  • Ok so this means that for each ios app their is a finite amount of memory and the objects which i don't release will be inside the memory even after the app termination and will not allow the other apps to run because they are consuming memory as i forgot to release them is this the case sir... – Radix May 04 '11 at 15:48
  • Just to make sure: When your application terminates, the game is over and all memory is returned to the system — there are no objects left in memory after that. All memory management is done to save memory usage *while the app is running*. – zoul May 04 '11 at 16:27
0

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.

Community
  • 1
  • 1
ingconti
  • 10,876
  • 3
  • 61
  • 48