简体   繁体   中英

How can I prevent MKMapView from eating up so much memory when the view controller is still in the view controllers stack, but is not visible?

I have a view controller with an MKMapView as a subview. This view controller in turn segues to a view controller, which in turn segues to another view controller. The MKMapView can bring my Apps memory footprint as high as 60mb.

While other view controllers are being presented, is there a way to reduce the footprint of the MKMapView without entirely removing it from its super view and deallocating it?

One way to accomplish this without altering your view hierarchy is to toggle the MKMapView 's mapType. This results in the MKMapView freeing all its map tiles and significantly reducing its memory footprint.

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    // Toggling this property forces the map view
    // to release its tiles, freeing up memory.
    self.mapView.mapType = MKMapTypeHybrid;
    self.mapView.mapType = MKMapTypeStandard;
}

When examining heap allocations in profile, I observed as much as a 30mb reduction in memory footprint.

Note: This trick will not work if done in viewDidDisappear , toggling mapType in viewDidDisappear will result in the tiles being freed upon returning to the view. Bonus points if somebody knows why?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM