Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone Development - Simulate Memory Warning

Background:

I have a tab bar application. Each tab contains navigation controller allowing the user to transition from one view to the other showing a drill down information of the data (each view is being handled by a view controller and each view controller class has didReceiveMemoryWarning method). Lists are populated by pulling the data from web services.

Problem:

When i use "Hardware > Simulate Memory Warning" option of iPhone Simulator, the didReceiveMemoryWarning method is called for ALL my view controllers - even the one which the user is viewing. I don't want to clear any content which is being used by the active view controller. How can I achieve that?

Which method should have the implementation to reload the data after the data was released because of memory warning? (I see that the view controller classes that contain a table view call viewDidLoad method when user comes back to that view, but if the view contains (say UIWebView) then viewDidLoad method is not called. Why is that?)

Edited (Friday 30 January 2009 - 03:10 PM)

(Note: I'm using Interface builder for creating views, and loadView method is commented out.)

So, when a view controller receives a memory warning message, these are the steps that are carried out:

  1. Following method is called:

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning]; 
    }
    
  2. As a result of call to [super didReceiveMemoryWarning], [self setView:nil] gets automatically called?

  3. If any resources should be cleared, then setView method should be overwritten to clear local resources.

  4. [self setView:nil] is not called if the view is currently active (By default). Right? - I'm really curious which method takes this decision and how?

Can you please confirm. Plus, I was getting an error following this approach but adding myObject = nil after releasing myObject in dealloc method of controller class fixed the issue. Thanks.

like image 651
Mustafa Avatar asked Jan 29 '09 10:01

Mustafa


People also ask

How do I simulate memory warning in Xcode?

To simulate the memory warning we need to enable the toggle when the app runs on the simulator. To enable the logger for memory warnings, please run the app onto the simulator and then go to Debug → Simulate Memory Warning.

How much RAM do I need for Xcode development?

Fast 512GB SSD for opening Xcode projects quickly (many small files) Fast 16 GB RAM large enough for nearly all developer workflows.

How do I simulate Xcode on my iPhone?

To launch a Simulator without running an appChoose Xcode > Open Developer Tool > Simulator. Control-click the Xcode icon in the Dock, and from the shortcut menu, choose Open Developer Tool > Simulator.

How do I simulate an iOS device?

For iOS, tvOS, and watchOS apps, you can choose a simulated device, under [Platform] Simulators, from the run destination menu next to the scheme menu in the toolbar. To add additional simulators of a product family running older versions of the operating system, choose Add Additional Simulators.


2 Answers

This is an old question, but I don't see a proper answer, so here goes:

When a memory warning is received, -didReceiveMemoryWarning gets called in ALL view controllers, whether they are the "current" one or not. The view controllers are simply listening for the memory warning event broadcast.

If the view controller's view isn't being used at the time of the memory warning, the controller will unload it by setting the property to nil. How does it know if the the view is used? By the view's -superview property. If view.superview is nil, the view isn't part of any tree and can be unloaded safely.

Once that happens, the controller's -viewDidUnload gets called. This is the correct place to unload any outlets, and anything that will get re-created in -viewDidLoad.


So what is -didReceiveMemoryWarning for? Your controller might have objects that don't get instanced until accessed. For example, you could have a controller that sometimes needs a big chunk of data from a file, but not always. You could have a property set for it like this:

- (NSData*)bigChunkOfData {
  // Get data from our instance variable _data, read from disk if necessary
  if (_data == nil) {
    _data = [[NSData alloc] initWithContentsOfFile:@"/path/to/data"];
  }
  return _data;
}

This will read the data from disk this first time, then keep it in an instance variable. Since the _data variable is created on demand, it's safe for us to unloaded it in low-memory situations: it'll just get created again next time we need it.

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];

  [_data release];
  _data = nil;  // <-- Very important: don't leave strong references dangling.
}
like image 194
xaethos Avatar answered Oct 04 '22 21:10

xaethos


I do my clean up like this:

-(void)setView:(UIView*)view
{
    [super setView:view];
    if(view == nil)
    {
       // Our view has been cleared, therefore we should clean up everything 
       // we are not currently using
....

setView:nil is called by UIViewController in response to a memory warning, if that view is not currently visible - which is basically what you want to know.

EDITED

In answer to the follow ups:

  1. Correct.
  2. That's what I do, and it works for me.
  3. Correct. The implementation of didReceiveMemoryWarning in UIViewController is what does this. If you don't override didReceiveMemoryWarning, then the base class implementation in UIViewController will be called - if you do override it, obviously you should call:

    [super didReceiveMemoryWarning]
    
like image 39
Airsource Ltd Avatar answered Oct 04 '22 20:10

Airsource Ltd