Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MKMapView using a lot of memory each time i load its view

I have a pretty simple app, with a couple of view controllers. There is a MKMapView in the second view controller. It is set up correctly, and functions fine. The problem is, each time I load its view the Memory usage jumps ~30mb, and never goes back down, so each time i go into the view it keeps jumping and eventually gets super high. I tried removing the map view when i leave the controller like this:

override func viewWillDisappear(animated: Bool) {
        map.removeFromSuperview()
    }

but it doesn't have any effect on the memory. The map views delegate is set to its view controller.

I tried checking for leaks using Xcode instruments but didn't find anything.

Does anyone know how to fix this?

Thanks

EDIT: Adding this seems to work:

func removeNastyMapMemory() {
        map.mapType = MKMapType.Hybrid
        map.delegate = nil
        map.removeFromSuperview()
        map = nil
    }

    override func viewWillDisappear(animated: Bool) {
        removeNastyMapMemory()
    }
like image 438
Bullwinkle Avatar asked Apr 05 '16 04:04

Bullwinkle


1 Answers

This is not Swift issue, is coming from Objective-C days. The possible ways to handle this issue is depending upon the situation and behavior of the app.

  1. If you're using a Map for multiple times (or places), only create a single (shared) instance of it. Which you can use it whenever you want.

  2. Or If you're only using it for once, then try a solution from here, https://stackoverflow.com/a/25419783/1603234. This may help. Reduce little. But not all.

like image 193
Hemang Avatar answered Nov 15 '22 22:11

Hemang