1

I am using MKMapView in Objective-C. App is taking 45MB memory, but when MKMapView appears memory usage increases up to 110 MB. This is not good iPhone 4 and iPhone 4S.

Please suggest me code structure for optimize memory.

1
  • Are you measuring memory usage on device or simulator? Commented Dec 8, 2015 at 10:20

2 Answers 2

10

Well working with MKMapKit need to be careful about Memory Management. Especially if you are using UITabbarController and setting MapView in any of the view controller.

If you need to use MKMapKit in your application i would recommend you to initialise MapView in viewWillAppear and remove from Superview in viewDidDisappear. Also you need to remove it's delegate mapView.delegate = nil and make MapView = nil before removing from Superview.

Objective-C

- (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; mapView.delegate = nil; [mapView removeFromSuperview]; mapView = nil; } 

swift

override func viewDidDisappear(animated: Bool) { super.viewDidDisappear(animated) mapView.delegate = nil mapView.removeFromSuperview() mapView = nil } 

By doing this all you memory occupied by map will be free.

Please do not add MapView in your ViewController in Storyboard or xib. Create MapView Programmatically.

Hope it helps you with Memory Management with MKMapKit.

Sign up to request clarification or add additional context in comments.

Comments

4

Are you using iOS 9 ?

I do this and I have no memory issues with my maps - in iOS 8 and below this was an issue and there are many threads on this topic.

So if you are in iOS 9, do this and see if it helps.

override func viewDidDisappear(animated: Bool) { super.viewDidDisappear(animated) mapView.removeFromSuperview() } 

3 Comments

thank for reply, but i have did that still memory issue.while ViewController is popOut and this viewDidDisappear will called but memonry is still same 110 MB
As long when you push and pop the memory doesn't keep climbing up. Previously (pre iOS 9) i would push / pop and the memory would climb and climb until crash. I just did a test with my app in simulator. App first load = 22 mb, present map 180-200 mb, pop map (modal view) down to 110mb. Repeat process, 180 = show map, 110 remove. Before it would go 180, 250, 300, 400 etc ... crash. I did this 20x in a row - no crash so I assume this is as good as it gets. Before, 10 times repeat process then I would get a crash.
I also follow the same step and try to create map view programmatically, avoid creating from storyboard and it will fix your issue

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.