最近使用MKMapView,发现有内存泄露。经查发现属iOS系统 bug,参见
http://stackoverflow.com/questions/16420018/memory-not-being-released-for-mkmapview-w-arc
http://stackoverflow.com/questions/26463125/memory-leak-in-mapkit-ios8
尽管如此还是有一些方式可以优化。
一、使用同一个MKMapView
因为每次创建一个 MKMapView,都会产生很多的内存泄露(具体要看操作的频次),所以如果整个 app只 holding 一个MKMapView,可以防止重复创建所产生的叠加内存泄露。
两种写法:
- 在 AppDelegate 里创建MKMapView;
使用静态变量:
static MKMapView *_mapView = nil;
if (!_mapView) { _mapView = [[MKMapView alloc] init]; }