2

If I am receiving memory warnings where exactly I have to release all my views & data ?

Whether I have to release in

- (void)didReceiveMemoryWarning or in

- (void)viewDidUnload

Tariq
  • 9,861
  • 12
  • 62
  • 103
  • -(void)viewDidUnload :) - because when OS start giving memory warning It try to remove view which is curruntly not using. while unloading view viewDidUnload method calls. there all view property should be set to nil. – iOSPawan Sep 08 '11 at 06:18

2 Answers2

1

For iPhone OS 3.0 and later viewDidUnload maybe called during low memory situtations so best to release views in viewDidUnload, just note that for custom views, create them at viewDidLoad instead of init method of the class.

didReceiveMemoryWarning is used more for releasing custom data structures instead of releasing views.

Manny
  • 6,277
  • 3
  • 31
  • 45
  • Okay so you mean to say all allocated UIButtons, UILabel, controller objects etc should release in viewDinUnload ? And all data should be release in didReceiveMemoryWarning only ? Right – Tariq Sep 08 '11 at 06:22
  • Yes, that's the idea. (But I can't help to say it: release also in dealloc! Although that's already given). Normally you'd release data that significantly consumes memory; e.g. bitmap stored in memory. For small data, like small NSArray's, I believe it's pointless unless they are great in numbers. – Manny Sep 08 '11 at 06:23
  • Okay thanks and one more thing I should not write down same data or same object in both viewDidUnload and memoryWarnin ? Then it will try to release two times and application will crash. Is it ? – Tariq Sep 08 '11 at 06:25
  • This does not happen always, but it's possible. That's why it's better to use self.myVariable = nil; instead of releasing it to avoid double releases; assuming you are using @property(nonatomic, retain) of course. – Manny Sep 08 '11 at 06:29
1
- (void)didReceiveMemoryWarning

This is supposed to be used only on things that you dont and wont need anymore so put here what is strictly innecessary, for instance if you are not using a view anymore and maybe it wasnt released you can release it here so when the memory warning comes it will be released.

- (void)viewDidUnload

Here you make sure that the views will be released in case they are referenced additionally by a view controller. You can refer to this question: When to use viewDidUnload

Community
  • 1
  • 1
Jose Luis
  • 3,307
  • 3
  • 36
  • 53