Skip to content

AGSMapView memory management

gdhakal edited this page Sep 18, 2014 · 3 revisions

Does the instances of AGSMapView get deallocated when we transition from one view controller to another view controller?


AGSMapView itself does not manage when it gets deallocated. That is up to its owning view controller, and any other objects that might have strong reference to it. As long as these references are alive, the map view will be alive.

Scenario I: Create a project with two view controllers and have a map view/AGSMapView on both of them. Create "Go to next" and "Go to previous" buttons to navigate between view controllers. Now, when you switch back and forth view controllers using these buttons, you will notice consistent increase in memory usage leading to application crash.

The reason you are seeing memory increase is because each time you switch to a different view controller, you are adding the view controller to the navigation controller’s stack. This stack keeps on growing with each push, and keeps all previous view controller alive in case you want to pop back to them.

Scenario II: If you add a navigation bar and pop back through the view controller list, you will notice that memory decrease as you navigate back.


How can the view controllers manage memory?

In essence, when the view controller gets a low memory warning (you can simulate this in the iOS simulator under the Hardware menu), the view controller should relinquish its view so that the complete view hierarchy (including the map view which is part of that hierarchy) can be deallocated.

The owning view controller should handle low memory conditions as described in this doc from Apple: Resource Management in View Controllers. Please refer to the section: On iOS 6 and Later, a View Controller Unloads Its Own Views When Desired.

- (void)didReceiveMemoryWarning {

 [super didReceiveMemoryWarning];
 // Add code to clean up any of your own resources that are no longer necessary.
 if ([self.view window] == nil) {
    // Add code to preserve data stored in the views that might be needed later.

    // Add code to clean up other strong references to the view in the view hierarchy.
    self.view = nil;
 }

}

Once you add the code to both the view controllers in the project, and push a few view controller onto the navigation controller, you will see that memory drops when you simulate low memory condition in the simulator. We recommend using leaks to monitor allocations so that you can see the number of living AGSMapView references and verify that the number actually drops when moving back through the view controller list, or simulating low memory conditions.


Thanks to Divesh Goyal for providing this information to Support Services.