4

The ARC is used in iOS and an object will be automatically deallocated when the reference count is 0. In the example of the swift book, the code sets some var to nil in order to make the reference count to be 0. In a real iOS app, I may not purposely set something to nil to release memory unless the object is really large.

My question is, when will deallocation in the ARC happen in the lifecycle of an app? I am considering two cases:

  1. If we segue from one viewcontroller to the next viewcontroller, will the first viewcontroller be deallocated automatically? Assume we do not retain a copy in the next viewcontroller.

  2. If the iOS somehow just killed our app, say it needs memory or something, does it just clear all the memory used by our app? Or it will set something to be nil and trigger the ARC to finish the work? If there is some strong reference cycle in the code, will there be memory leakage in this case?

Anson Yao
  • 1,544
  • 17
  • 27
  • Thanks for providing resources. But I cannot answer either case 1 or case 2 after I read them...ARC for memory management is not new. I am just wondering how it is used in the lifecycle of an iOS app. – Anson Yao Jul 23 '15 at 16:16
  • If you want to ask about a specific scenario, I recommend asking about just that single specific scenario. Otherwise, your question is a bit broad. – nhgrif Jul 23 '15 at 16:21

2 Answers2

1

It is my understanding view controllers are kept on a stack and controllers that are not popped off the stack retain their objects.

Strong reference cycles can lead to memory leaks or to just using more memory than is currently needed after something containing that object is released.

Fred Faust
  • 6,696
  • 4
  • 32
  • 55
  • Thanks for the answer. I agree with you for the first case. But the second case is still unclear... – Anson Yao Jul 23 '15 at 15:57
  • If you have a strong reference cycle to a variable in a class and the class is released, it may still be allocated in memory along with the variable. I read about that in the Big Nerd Ranch Cocoa programming book, though it was quite some time ago. – Fred Faust Jul 23 '15 at 16:02
1
  1. Its already answered here, the first view controller will not be deallocated if it pushes/presents another controller.
  2. If iOS kills your app, all the memory holds by your app will be released. So next when you open your app, it will be a fresh launch. And there is something called memory warnings, when device running out of memory or your app consumes lot of memory OS will send out memory warnings. Implement the didReceiveMemoryWarning method and release any un used data or resources. It may save your app from killing by the OS.
Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
  • I see. So basically the ARC is triggered by ourselves in the didReceiveMemoryWarning function by setting references to nil, right? Moreover, if the app is really killed, the memory is all cleared regardless of the reference cycle, right? – Anson Yao Jul 23 '15 at 18:17
  • 1
    @AnsonYao ARC is not triggered by ourself, OS is taking care of memory management there. To be more clear let us assume a scenario, VC A pushed/presented VC B and when the user dismiss VC B it will be automatically deallocated(controller&all its variables) unless there is a retain cycle – Anil Varghese Jul 24 '15 at 05:02