GMSMapView 清除不清除内存



我的应用程序中有一个GMSMapView。具有打开地图屏幕的应用程序占用 150 MB 内存。

在某些时候,我添加了很多多边形。之后,应用程序需要 230 mb。

调用该方法 [GMSMapView 清除] 后,所有多边形都消失了,但应用程序仍占用 230 MB 的内存。

指向多边形的指针不会存储在其他任何位置。 如何使地图清除内存以及为什么调用"清除"方法后没有发生?

嗯,这是痛苦的**...我没有找到官方文档。

我的解决方案:

斯威夫特 3.0

1(不要使用故事板,在代码中创建mapView:

@IBOutlet weak fileprivate var mapViewContainer: UIView!
weak fileprivate var mapView: GMSMapView? {
didSet { 
//you can config and customise your map here
self.configureMapView() 
}
}
//If you use clustering
fileprivate var clusterManager: ClusterManager?
fileprivate var clusterRenderer: ClusterRenderer?

2(创建viewController配置函数并在viewDidLoad之前调用它:

func configure() {
mapView = GMSMapView()
mapView?.delegate = self
}

3(布局并将mapView添加到您的UI中:

override func viewDidLoad() {
super.viewDidLoad()
if let mapView = mapView {
mapViewContainer.addSubview(mapView)
//Create constraints as you wish
mapView.fillView()
mapViewContainer.layoutSubviews()
}
}

4(现在创建函数:

func releaseMemory() {
if
let mapView = mapView,
let clusterManager = clusterManager {
clusterManager.clearItems()
mapView.clear()
mapView.delegate = nil
mapView.removeFromSuperview()
}
clusterManager = nil
clusterRenderer = nil
mapView = nil
}

您可以在 ViewDidDisappear 中调用它,或调用 delegate...

releaseMemory()功能清除记忆,不是那么聪明,但它有效。

您可以尝试将面对象存储在某个位置,然后对所有对象调用polygon.map = nil,删除面引用,然后调用地图视图clear

最新更新